Types and expressions
Choose a type for each input or stored value. Use expressions to compute a result, test a condition, or insert a value into a message.
On this page
Types#
| Type | Written | Notes |
|---|---|---|
Int, Float, Bool | 42, 1.5, true | Int division truncates; an Int widens to Float, never the reverse without int() |
Int8 … Int64, UInt8 … UInt64, Size, Float64 | type = UInt8 | exact sizes (Int is Int64, a 64-bit whole number from -9223372036854775808 to 9223372036854775807; Float is 32-bit, up to about 3.4028235e38); a literal must fit the kind it fills (see below); different kinds never mix: convert with uint8(x), size(x), float64(x) …; arithmetic wraps; decimals convert to whole numbers by cutting toward zero, out of range taking the nearest end |
Bytes | type = Bytes, size = 4096 | any bytes, zero included, up to size: bytes("text") stores text, text(b) reads it back (to the first zero byte), length(b), b[i] is one UInt8; past the size or the length the program stops and prints the file, line and column of the expression that failed; a Bytes cannot be a struct field, so keep it in a value |
Char | "x" where a Char is expected | one character |
String | "text ${expr}" | with size = N it is a buffer that set, append and read_line can fill |
Array | type = Array, of = Int, size = 10 | fixed length; of may be a struct or an enum |
List | type = List, of = Guest, size = 32 | growable up to size; add, remove, empty, length() |
Pointer | type = Pointer, to = Int | address_of(x) and value_at(p) |
File | type = File | set by open, closed automatically |
| enum | ability "enum" "Mood" { values = [Happy, Sleepy] } | values are Mood.Happy |
| struct | ability "struct" "Guest" { fields = [...] } | values are { name = "Ada", age = 36 } |
| class | ability "class" "Counter" { fields = [...], methods = [...] } | a struct with methods; see Classes and interfaces |
| interface | ability "interface" "Shape" { methods = [...] } | only as a function parameter's type |
| alias | ability "type" "Entity" { is = Int } | another name for a basic type |
| provider types | Widget, Key, Color… | declared by a provider as a handle or an enum |
Number ranges. A number literal is checked against the kind it fills wherever it is written: a variable default, initial, a constant, a struct field default, an array element, a module input, a call argument, set and return. A literal outside the range is refused by structop validate; it never reaches the compiler or wraps. A struct field default of the wrong type ("abc" for a UInt8, 5 for a Bool) is refused too.
Error: initial for ability.value.small must fit in UInt8 (0 to 255), got 300 on main.st line 3, column 15
A whole-number expression made only of literals is worked out before the check: 1 + 2 fits a UInt8 parameter, while 200 + 100 does not (200 + 100 is 300, which does not fit in UInt8 (0 to 255)). Every part must fit as well, so 300 - 100 is refused for a UInt8 although its value is 200. Division by zero is an error. This applies to the exact kinds (UInt8 … UInt64, Int8 … Int32, Size); an expression that contains a variable, a function or a Float is not worked out, and plain Int arithmetic is not either.
A value of another number kind is not accepted silently. An Int passed to a UInt8 parameter is refused with argument 1 is Int but params want UInt8; convert with uint8(...). Write the conversion: uint8(x), int8(x), int16(x), int32(x), int64(x), uint16(x), uint32(x), uint64(x), size(x), float64(x), int(x) or float(x). A whole-number literal that fits still adapts, so f(5) needs no conversion.
Types may be used before, or in another file than, their declaration; only structs that contain each other are refused. A struct field can itself be an array: { name = "levels", type = Array, of = Bool, size = 3 }, read and written as t.levels[i], sized by length(t.levels), and given as a list in struct values (levels = [true, false, true], or struct values inside the list). Fields left out of a struct value take their defaults. A whole struct can be assigned (set target = ability.value.player value = { x = 5, y = 25 }), and functions may have struct locals.
Expressions#
Logic is never quoted. StructOp parses, types and checks every expression, then the provider prints it.
condition = data.value.player.current.y > const.SCREEN_HEIGHT || hit_obstacle()
value = min(health.max, health.current + 1)
message = "Score: ${data.value.score.current} (${int(var.speed * 10)}%)"Each attribute determines the type it accepts. In these examples, the condition evaluates to a boolean, the next line computes a number, and the message inserts a number into a string. Use the expression reference to look up operators, conversions and binary operations.
See operator precedence for a working calculation or arrays of structs for typed records. For methods on a struct, see Classes and interfaces.