Builtin cast targets include eleven fixed numeric types:
I8, I16, I32, I64U8, U16, U32, U64F16, F32, F64
Byte, Dec and Dec0 through Dec256 remain separate targets. Bool, String, Char and Error also have builtin conversion policies.
Same-type casts are invalid. Byte remains distinct from U8, and no cast silently changes one into the other.
The target comes from an immediate typed destination such as an annotation, return slot, concrete parameter, field or typed collection entry. An unresolved generic slot and a distant later use cannot supply it.
Not every source-target pair can convert. Numeric pairs follow their complete types, not a value the compiler happens to know. See Cast
targets for exactness, rounding and failure rules.
cast expression needs a target from its immediate concrete receiving boundary. The target does not choose the source type. See Cast syntax for the boundary rules.
Builtin targets
The eleven fixed numeric targets are:
- Integers:
I8, I16, I32, I64, U8, U16, U32, U64 - Binary floats:
F16, F32, F64
Builtin conversion policies also cover Byte, Dec, Dec0, Dec1 through Dec256, Bool, String, Char and Error. A target name being valid does not make every source-target pair valid.
Fixed numeric conversions
Fallibility follows the complete source and target types, not a known value:
- Fixed integer-to-integer conversion preserves the value. It is infallible when the complete source range fits the target and fallible otherwise.
- Fixed integer-to-
F32 or F64 conversion is infallible and may round. Conversion goes directly to the destination precision. - Fixed integer-to-
F16 is infallible only when the complete source range fits the finite F16 range. Otherwise it is fallible. - Binary-float precision widening is infallible. Narrowing is fallible because a rounded result may become non-finite. Round directly to the destination, nearest with ties to even. Precision loss alone succeeds.
- Binary-float-to-integer conversion is fallible. Truncate towards zero, then check the target range.
-0.5 truncates to zero and can convert to an unsigned target; -1.0 fails.
For example, U8 -> U16 is infallible, U16 -> U8 is fallible and F64 -> F16 is a direct narrowing conversion, never an F64 -> F32 -> F16 chain. U64 -> F32 converts directly without an intermediate binary64 rounding step.
For the complete U64 value 9007199791611905, direct conversion to F32 produces bits 0x5a000001. An intermediate F64 conversion would collapse the value to 0x5a000000.
Integer-to-Dec and scale widening are exact and infallible. Scale narrowing and Dec-to-integer conversions are exact or fail: removed fractional digits must be zero and the full integer value must fit its target. Dec does not convert directly to or from a binary float.
Byte and U8 convert infallibly in both directions. Other numeric conversions involving Byte compose through U8. Byte remains distinct from U8; no receiving boundary silently changes one into the other.
Byte has no direct conversion to Bool or Char.
Text and other builtin conversions
String parses the complete numeric grammar for fixed integers, binary floats, Byte and Dec targets. These conversions can fail.- Every numeric type formats to
String infallibly using its canonical text. Byte composes through U8. String -> Bool trims surrounding whitespace and accepts only lowercase true or false. It can fail.String -> Char requires exactly one Unicode scalar and can fail.Char -> I32 returns the Unicode scalar code point. I32 -> Char can fail if the value is not a Unicode scalar.Bool -> String and Char -> String are infallible.String -> Error creates an error message with code 0. Error -> String returns only the message. Both are infallible.
Same-type casts are invalid. No cast acts as an identity conversion.
No other source-target pair follows from this list. In particular, Dec and binary floats have no implicit mixing or direct cast pair.
Exactness and numeric text
A cast does not promise universal exactness. Integer-to-integer conversions preserve values. Binary-float conversions round to nearest, ties-to-even. Binary-float-to-integer conversions truncate towards zero before the range check. Decimal scale narrowing and decimal-to-integer conversions require exactness and never round.
Numeric text consumes the whole string. Parsing rejects surrounding whitespace, unary +, uppercase exponent markers, malformed separators or exponents, NaN and infinity spellings. Unsigned integer targets also reject a leading minus, including -0. A grammar-valid float spelling still fails if its rounded result becomes non-finite.
Binary-float formatting normalises negative zero to 0 and uses lowercase exponents when exponential notation is needed. Decimal formatting trims fractional trailing zeroes, uses a leading zero before fractions and never emits exponent notation or negative zero. Unsigned integer formatting uses base ten and writes zero as 0.
No modulo, wrapping or saturating conversion exists.
Explicit casts retain these checks inside any future fast-math scope. Such a scope cannot relax conversion range, exactness or finite-result rules.
Optional receiving contexts
A T? receiver casts to inner T, then applies ordinary optional wrapping. The recovery handler also produces T; it cannot use then none. Optional source values never unwrap automatically.
parse_optional |text String| -> I32?:
value I32 = cast text catch:
return none
;
return value
;
The Progress matrix owns current compiler and backend support: Progress.