Mixed-number arithmetic looks effortless: `3 + 4.2` produces `7.2`. Behind that result, the values need a common numeric type. Data conversion changes a value from one related numeric type to another; promotion decides which type mixed values should share. Parsing a string into a number is a different operation. section 9.0 section 9.2
Julia provides two ways to request a conversion:
- Call the target type as a constructor: `Float64(1//2)` gives `0.5`, and `Int8(4.0)` gives `4`. This is the simplest approach for an explicit conversion. section 9.2
- Call `convert(T, x)`: `convert(Float64, 5)` gives `5.0`. Julia also calls `convert` implicitly when assigning to a typed array element or composite-type field, or when satisfying an annotated local variable or return type. section 9.2
- Expect a failure when the destination cannot represent the value: `Int8(1000)` and `Int64(4.5)` both raise `InexactError`. section 9.2
Convert an angle before using it
Suppose `DMS` stores an angle as a count of arcseconds and `Radian` stores radians. A constructor can calculate radians from DMS; a matching `convert` method makes that route available to Julia’s conversion machinery: `import Base: convert` `Radian(dms::DMS) = Radian(deg2rad(dms.seconds / 3600))` `convert(::Type{Radian}, dms::DMS) = Radian(dms)` Angle-aware `sin` and `cos` methods can then accept explicit angle units rather than leaving a plain number’s unit ambiguous. section 9.3 section 9.3.5
Listing 9.11 Simplifying arithmetic operations by utilizing promotion
+(Θ::Angle, α::Angle) = +(promote(Θ, α)...) -(Θ::Angle, α::Angle) = -(promote(Θ, α)...)
Delegating angle arithmetic to `promote` avoids writing an operator method for every pair of units. A `promote_rule` method can select `Radian` for a `Radian`–`DMS` pair; the corresponding conversions supply values of that type. section 9.3.7




















