- Julia 1.0 Programming Complete Reference Guide
- Ivo Balbaert Adrian Salceanu
- 409字
- 2021-06-24 14:21:49
Type conversions and promotions
The convert function can also be used explicitly in the code as convert(Int64, 7.0), which returns 7.
In general, convert(Type, x) will attempt to put the x value in an instance of Type. In most cases, type(x) will also do the trick, as in Int64(7.0), returning 7.
The conversion, however, doesn't always work:
- When precision is lost—Int64(7.01)returns an ERROR: InexactError() error message
- When the target type is incompatible with the source value—convert(Int64, "CV") returns an ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64 error message
This last error message really shows us how multiple dispatch works; the types of the input arguments are matched against the methods available for that function.
We can define our own conversions by providing new methods for the convert function. For example, for information on how to do this, refer to http://docs.julialang.org/en/latest/manual/conversion-and-promotion/#conversion.
Julia has a built-in system called automatic type promotion to promote arguments of mathematical operators and assignments to a common type: in 4 + 3.14, the integer 4 is promoted to a Float64 value, so that the addition can take place and results in 7.140000000000001. In general, promotion refers to the conversion of values of different types to one common type. This can be done with the promote function, which takes a number of arguments, and returns a tuple of the same values, converting them to a common type. An exception is thrown if promotion is not possible. Some examples are as follows:
- promote(1, 2.5, 3//4) returns (1.0, 2.5, 0.75)
- promote(1.5, im) returns (1.5 + 0.0im, 0.0 + 1.0im)
- promote(true, 1.0) returns (1.0, 1.0)
Thanks to the automatic type promotion system for numbers, Julia doesn't have to define, for example, the + operator for any combinations of numeric types. Instead, it is defined as +(x::Number, y::Number) = +(promote(x,y)...).
It basically says: first, promote the arguments to a common type, and then perform the addition. Number is a common supertype for all values of numeric types. To determine the common promotion type of the two types, use promote_type(Int8, UInt16) to find whether it returns UInt16.
This is because, somewhere in the standard library, the following promote_rule function was defined as promote_rule(::Type{Int8}, ::Type{Uint16}) = UInt16.
You can take a look at how promoting is defined in the source code Julia in base/promotion.jl. These kinds of promotion rules can be defined for your own types too if needed.
- Learning LibGDX Game Development(Second Edition)
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- Getting Started with Gulp(Second Edition)
- Software Defined Networking with OpenFlow
- Java范例大全
- CouchDB and PHP Web Development Beginner’s Guide
- 精通Python自然語言處理
- PhpStorm Cookbook
- Mastering openFrameworks:Creative Coding Demystified
- SSM開發實戰教程(Spring+Spring MVC+MyBatis)
- PhoneGap 4 Mobile Application Development Cookbook
- C語言開發寶典
- Unity AI Game Programming(Second Edition)
- Visual C++ 2017網絡編程實戰
- 3D Printing Designs:Fun and Functional Projects