Types
Inference
Section titled “Inference”Kura infers types where it can. You only need annotations to constrain a value or document an API:
let x = 10 // inferred: Intlet y: Float = 10 // 10.0let z: Seq<Int> = [ 1, 2 ]If the compiler can’t determine a type, or an annotation doesn’t match the value, it reports an error.
Numeric types
Section titled “Numeric types”Kura has width-specific numeric types so the compiler can choose efficient machine representations.
| Type | Meaning |
|---|---|
Int8, Int16, Int32, Int64 |
Signed integers |
UInt8, UInt16, UInt32, UInt64 |
Unsigned integers |
Float32, Float64 |
Floating point |
Int |
Alias for Int64 |
Float |
Alias for Float32 |
An unsuffixed integer literal (10) is an Int; a literal with a decimal point (10.0)
is a Float.
Range checking
Section titled “Range checking”A literal that doesn’t fit its annotated type is a compile error:
let a: UInt8 = 300 // error: 300 doesn't fit in UInt8let b: Int = 300 // finelet c = 10 // fine: fits an IntImplicit conversion
Section titled “Implicit conversion”Kura converts numeric values to the type the context asks for, so you don’t write casts for ordinary arithmetic:
let x: Float = 10 // becomes 10.0
fn add(x: Int, y: Int): Float => x + yadd(10, 10) // 20.0, no explicit castWhen a value crosses the C boundary, Kura converts it to the C type automatically (an
Int32 becomes a C int32_t). A value that doesn’t fit the C type is a panic. See
C Interop.
Measurements
Section titled “Measurements”Px, Pct and Fr are distinct types, not numbers. Arithmetic is checked against the
unit, and you cast explicitly to get the underlying number. See
Measurements.
Geometry
Section titled “Geometry”Vec2, Vec3 and Rect are the basic geometry the compiler, the runtime and the renderers share: a point or a
size in the plane, a point in space, and a rectangle by its top left corner and size. They are made of 32-bit
floats, laid out exactly like the renderer ABI’s KuraVec2, KuraVec3 and KuraRect, so a Rect is handed to a
renderer as it is.
From a list
Section titled “From a list”A list of numbers is converted to geometry wherever the geometry is asked for, with no cast:
| Type | The list is |
|---|---|
Vec2 |
2 numbers: x, y |
Vec3 |
3 numbers: x, y, z |
Rect |
4 numbers: x, y, width, height |
fn center(): Vec2 => [100, 50]
fn draw-box(r: Rect): Int => 1
fn demo(): Int => draw-box([0.0, 0.0, 200.0, 100.0])The numbers have to be of one type, all Int or all Float (an Int list is converted to floats), and there have
to be as many as the geometry takes. A list that is written out is checked as the program is compiled, and a list
that comes from anywhere else, or from a value of any type, is checked when it is converted: a list of the wrong
length, a list that mixes Int and Float, or one that holds anything but numbers is a panic that says which.
The conversion is where the geometry is asked for: as an argument, as a function’s result, and as the value of a
top-level let that has a type. A list that is stored in a record, or in another list, is stored as it is, and is
converted where it is used as geometry.
To a list
Section titled “To a list”To a host, and wherever a value of any type goes, geometry is the list of its floats: a Rect that a function
returns is [0.0, 0.0, 200.0, 100.0]. A host that passes one to a function that takes geometry passes that list.
The compiler itself holds geometry unboxed, as vectors of floats.
For example, the draw calls the compiler makes for a box take the box as a Rect, read once from the layout, where
they used to take its four coordinates one by one.
There is no literal for them, and nothing that reads one apart (no .x) yet.
Type declarations
Section titled “Type declarations”type declares record shapes, aliases and algebraic types:
type Position = { x: Int, y: Int, z?: Int }type Message = Click(Position) | Tick | Set(Int)type Op = (Int, Int) => Inttype Content = Node | NoneThe ? suffix on a field is sugar for Optional<T>.
Generic types
Section titled “Generic types”type Optional<T> = Some(T) | Nonetype Result<T, E> = Ok(T) | Error(E)Content = Node | None is equivalent to Optional<Node>. Some(T) is mostly useful in
patterns:
match m { Some(Click(p)) => 1, Some(other) => 2, None => 3,}Records are statically shaped
Section titled “Records are statically shaped”Records are typed, and their shape is known at compile time. That is what lets the compiler lay them out in memory, pass them across the C boundary and keep them POD. See Primitives.