Skip to content

Type Conversions

Ethan Jackwitz edited this page Jun 14, 2018 · 5 revisions

Type Conversion rules

Terms

  • Conversion refers to any change from one type to another.
  • Coercion refers to implicit conversions.
  • Casting refers to explicit conversions.
  • Max Value is the largest representable value for Integers. For Enums the Max Value is the value assigned to the last case.
  • Numeric refers to both Integer & Float types.

Coercion (Implicit)

Rules

Source Type(s) Target Type Condition(s)
All Any
All Union Target Union contains a member of Source Type
Numeric, Pointer Boolean
Integer Float
Integer Integer Sign matches and Target is Larger
Signed Unsigned Source is a positive constant less than Target's Max Value
Unsigned Signed Target's Max Value is larger than the Source's
Source is a constant less than Target's Max Value
Pointer Pointer Either Source or Target is Rawptr
Enum Integer Target's Max Value is larger than the Source's

Cast (Explicit)

Rules

Source Type(s) Target Type Condition(s)
All Any
All Union Union contains a member of Source Type
Numeric, Pointer Boolean
Numeric Numeric
Pointer Pointer
Pointer Intptr, Uintptr
Intptr, Uintptr Pointer
Function Pointer
Pointer Function
Enum Integer
Integer Enum

Notes

  • When converting between Signed integer types Sign extension to the target type occurs.
  • When converting from Signed to Unsigned integers the memory is simply reinterpreted. This has no runtime cost.
  • When converting from a Float to an Integer the fraction is discarded. (round down)
  • When converting to a Float type the value is rounded to the precision of that type.
    • Note that this is the only case where loss of information may occur during coercion.
Clone this wiki locally