When several angle units should work in one expression, writing a separate operator for every type pair quickly becomes unmanageable. promote_rule provides the shared promotion decision that lets generic arithmetic handle those combinations. section 9.3.7
promote_rule takes two type objects and returns the type to which both values should be promoted. Its methods use multiple dispatch to select a rule based on the supplied types, rather than inspecting individual values. section 9.3.7
Promoting radians and DMS angles
Register the function from Base, then declare that Radian is the common type for Radian and DMS: import Base: promote_rule; promote_rule(::Type{Radian}, ::Type{DMS}) = Radian. A generic method can then delegate addition and subtraction to promote(Θ, α), allowing expressions such as 45° + 3.14rad/4. section 9.3.7
A custom promotion design follows this pattern:
- Choose the common result type for each supported pair of types. section 9.3.7
- Define promote_rule methods whose arguments are type objects. section 9.3.7
- Use generic operations that call promote instead of multiplying pairwise operator methods. section 9.3.7
Listing 9.12 Defining type promotion of Radian and DMS
import Base: promote_rule
promote_rule(::Type{Radian}, ::Type{DMS}) = Radian
Inspect the compact Julia definition that registers Radian as the promotion target for DMS and Radian values. section 9.3.7
