Type Casts
The as operator converts a value from one type to another — an explicit cast. Rux performs no implicit conversions between distinct types, so wherever a value's type does not already match what is required, an as cast makes the conversion visible in the source.
let n: int64 = bigValue as int64;The left operand is the value to convert; the right operand is the target type. The result is the value expressed in the target type.
Numeric Conversions
Arithmetic and comparison operands must share a type. Convert one side explicitly when the types differ:
let left: int32 = 10;
let right: int64 = 20;
let invalid = left < right; // error: different operand types
let valid = (left as int64) < right; // OKSigned and unsigned integers also require an explicit cast. Choose a target type wide enough to represent the value correctly:
let count: uint32 = 100;
let delta: int32 = count as int32;Boolean Conversions
Boolean types are distinct from numeric types, so conversion in either direction is explicit. Conversion between boolean widths also uses as:
let wide: bool32 = flag as bool32;Casting an integer to bool is legal but easy to misuse. When deriving a boolean from a number, prefer an explicit comparison — it states the intent directly:
let active = n != 0; // preferred — clear and explicit
let opaque = n as bool; // legal, but the intent is less obviousSee Boolean Types for the full conversion rules.
Casting After a Type Test
as is the companion of the is type test. Test the type first, then cast inside the branch where the test has already succeeded:
if shape is Circle {
let circle = shape as Circle; // safe — the test just succeeded
Print(circle.radius);
}as vs. is
as and is are companion type operators:
| Operator | Question it answers | Result |
|---|---|---|
as | Give me this value as type T | the value as T |
is | Does this value have type T? | bool |
Precedence
as shares precedence level 2 with is — just below the unary operators and above arithmetic, so it binds tighter than the surrounding operators. This means left as int64 < right reads as (left as int64) < right:
let valid = left as int64 < right;Parentheses are still worth adding when they make the intent clearer. See Operator Precedence for the complete table.
See Also
- Type Tests — the companion
isoperator - Arithmetic Operations — where mismatched numeric types need a cast
- Primitive Data Types — the types you convert between