Skip to content

Built-in Types and Casts

Kelvra has explicit integer and floating-point families in addition to its collection, function, user-defined, and nullable types.

FamilyTypes
Signed integersi8, i16, i32, i64
Unsigned integersu8, u16, u32, u64, usize
Floating pointf32, f64
Other scalar typesbool, 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 = 255u8
var count i64 = 1000i64
var index usize = 4usize
var ratio f32 = 0.5f32
var precise f64 = 0.5f64

u is shorthand for the u32 literal suffix.

Use as for conversions that should be visible in source:

var large i64 = 42i64
var small i32 = large as i32
var ratio f64 = small as f64
var label str = small as str

Kelvra 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.

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)
}