Built-in Types and Casts
Kelvra has explicit integer and floating-point families in addition to its collection, function, user-defined, and nullable types.
Scalar types
Section titled “Scalar types”| Family | Types |
|---|---|
| Signed integers | i8, i16, i32, i64 |
| Unsigned integers | u8, u16, u32, u64, usize |
| Floating point | f32, f64 |
| Other scalar types | bool, str, null, void |
An integer literal without a suffix is i32; a decimal literal without a
suffix is f64. Add a suffix when the exact representation matters:
var byte u8 = 255u8var count i64 = 1000i64var index usize = 4usizevar ratio f32 = 0.5f32var precise f64 = 0.5f64u is shorthand for the u32 literal suffix.
Explicit casts
Section titled “Explicit casts”Use as for conversions that should be visible in source:
var large i64 = 42i64var small i32 = large as i32var ratio f64 = small as f64var label str = small as strKelvra permits explicit casts between numeric types and from a numeric type to
str. Related user-defined types may also be cast; a downcast is checked at
runtime and fails if the value is not an instance of the target type.
The as token must remain on the same line as the expression it continues.
The any type
Section titled “The any type”any is the dynamic boundary type. It is assignable to and from other types
and is available in source signatures, including package API declarations. Use
it for interop points that genuinely accept heterogeneous Kelvra values; prefer a
specific type everywhere else so the checker can protect callers.
fn Describe(value any) str { return type(value) + ": " + str(value)}Composite types
Section titled “Composite types”Array<T>,Set<T>, andDict<K, V>are covered in Collections.fn(T) Rdescribes function values; see Functions and Closures.T?makes a type nullable; see Nullability and Type Checking.- User-defined structs and aliases are covered in Types and Inheritance.