liveBook · Manning

Promotion

As explained in Julia as a Second Language

A promotion rule specifies how Julia determines a common type when values of different types participate in an operation.

When an expression combines different number types, Julia determines a sensible common type before performing the operation. This process is promotion: it keeps arithmetic predictable without requiring every operation to implement every type combination separately. section 9.0 section 9.1

Mixed numeric values

Julia promotes the operands in `3 + 4.2` to a compatible floating-point representation and returns `7.2`. It also handles combinations such as `UInt8(5) + Int128(3e6)` and `1//4 + 0.25`.

Promotion is implemented through ordinary Julia functions rather than compiler-hardwired rules. Arithmetic methods call `promote`, which produces values of a common type before the final operation runs. Developers can extend the system by adding methods to `promote_rule` and related conversion functions. section 9.1 section 9.4

Inspect promotion directly with representative values:

  • Use `@edit` to inspect the standard-library definitions behind arithmetic and promotion. section 9.1

Promotion supports custom numeric abstractions, including units such as degrees, radians, meters, feet, Celsius, and Fahrenheit. Combining promotion with conversion can make numerical code safer and support specialized accuracy, memory, or performance needs. section 9.0 section 9.4

How Julia as a Second Language covers it

How deep it goes
Explains the runtime rationale, standard-library implementation, `promote` behavior, and extension through `promote_rule`.
Examples
Demonstrates mixed arithmetic, direct `promote` calls, type inspection, and `@edit` exploration of operator definitions.
What it assumes
Assumes familiarity with values and types while distinguishing promotion from explicit conversion and text parsing.
What you can do after
Build it from scratch by defining promotion and conversion rules for custom numeric types and physical units.
How it connects
Connects promotion with conversion, custom number types, units systems, arithmetic operators, and standard-library source inspection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.