Types
Primitive Types
| Type | Description | Examples |
|---|---|---|
Int | 64-bit signed integer (overflow is a runtime error) | 42, -7, 0xFF, 0b1010 |
Float | 64-bit floating-point, guaranteed finite | 3.14, -0.5, 1e5, 2.5e-3 |
Bool | Boolean | true, false |
String | UTF-8 string with interpolation | "hello", "age: {n}" |
() | The unit type: no meaningful value | () (returned by println) |
No implicit conversions. Use Float.from_int() or float.to_int() explicitly.
Numeric Literals
A _ between two digits is a visual separator: 1_000_000, 0xFF_FF.
A number has one spelling. The prefixes 0x and 0b and the exponent
e are written in lower case (the digits of a hex number may be of
either case), an exponent has no +, a _ stands only between two
digits, neither a decimal number nor an exponent has a leading
zero (7, not 007, and 1e7, not 1e007), and an exponent is not
zero, which says nothing: 1.0 and 1.5, not 1e0, 1e-0 or
1.5e0. Any other spelling is an error that writes the number in its
one spelling. The one exception:
a fraction may end in zeros, so 1.50 can say it is a price. It is the
Float 1.5, and prints as 1.5.
-- Decimal
let n = 42
let big = 1_000_000
-- Hex and binary (always Int)
let mask = 0xFF
let flags = 0b1010_0001
-- Scientific notation (always Float)
let avogadro = 6.022e23
let tiny = 1e-9
let hundred = 1e2 -- Float(100.0), not Int
Scientific notation always produces a Float, even when the value is a whole number.
A Float is always printed with a decimal point and without an exponent, so a whole one ends in .0: println(1e2) prints 100.0.
Non-finite results like 1e999 are rejected at compile time.
Numeric Safety
silt treats silent wrong answers as worse than crashes. The numeric types are designed so that every value in Int and Float is a finite, ordinary number.
Integer overflow
Int is 64-bit signed. Arithmetic that would overflow is a runtime error, not silent wrapping:
println(9223372036854775807 + 1) -- runtime error: integer overflow
let min = -9223372036854775808 -- the smallest Int
println(int.abs(min)) -- runtime error: integer overflow: abs(-9223372036854775808)
A program that must go on when a number it was given does not fit
asks first: int.checked_add, int.checked_sub, int.checked_mul,
int.checked_div and int.checked_rem give None where the operator
would end the program
(int).
import int
fn main() {
println(int.checked_mul(999999999999999999, 100)) -- None
println(int.checked_add(40, 2)) -- Some(42)
}open in playgroundThe smallest Int is written -9223372036854775808: the minus sign and
the digits are one literal there. Without the minus sign,
9223372036854775808 is one more than the largest Int and is rejected
(number literal too large).
Finite floats
A Float is always finite: never NaN, never infinite. An operation whose result would be NaN or infinite is a runtime error that names the operation, matching the integer rule:
println(1.0 / 2.0) -- 0.5
println(1.0 / 0.0) -- runtime error: float division by zero
println(float.max_value * 2.0) -- runtime error: float overflow
println(math.sqrt(-4.0)) -- runtime error: math.sqrt of a negative number: -4.0
The same holds for every function that returns a Float: math.log of a number that is not positive, math.asin / math.acos outside -1..1, math.pow and math.exp overflow, and list.sum / list.product of Floats overflow all raise. float.parse returns an Err for "inf", "NaN" and out-of-range literals, and decoding a non-finite number into a Float field (toml.parse) is an error.
Where an input can be out of range, guard it explicitly:
let ratio = match total {
0.0 -> 0.0
_ -> part / total
}
Because every Float is finite, floats compare, sort and hash as ordinary numbers and work as map keys and set elements. -0.0 is the same value as 0.0: 0.0 * -1.0 prints 0.0.
No implicit coercion
There are no implicit conversions between Int and Float. Convert explicitly with Float.from_int(n) and float.to_int(x).
Enums (Tagged Unions)
type Shape {
Circle(Float),
Rect(Float, Float),
}
type Color {
Red,
Green,
Blue,
}
Constructors create values: Circle(5.0), Rect(3.0, 4.0). The compiler
checks exhaustiveness when you match on them.
Generic Types
type Option(a) {
Some(a),
None,
}
type Result(a, e) {
Ok(a),
Err(e),
}
Type parameters are filled in at use: Option(Int), Result(String, String).
Records
type User {
name: String,
age: Int,
active: Bool,
}
fn main() {
let alice = User { name: "Alice", age: 30, active: true }
println(alice.name) -- Alice
}open in playgroundA record has at least one field: type Void {} is an error. A type of
one value is an enum of one variant, type Void { Void }, whose value
is Void.
A field written alone takes the value of the name in scope: User { name, age, active: true } is User { name: name, age: age, active: true },
as the record pattern User { name, age, .. } binds name and age.
Building and reading a record look alike. A record literal starts with
the name of its type: braces alone, { name }, are a block, and
{ name: "A" } is an error.
A field is read on a value whose type is known where it is read, or
later in the same function. A function that reads a field of a
parameter names the parameter’s type,
fn age(u: User) -> Int { u.age }: a field of a value whose type
nothing in the function decides is an error that asks for the
annotation.
A literal reads the same in the header of a match:
match User { name } { ... } matches on the literal, and the {
behind it opens the arms.
Records declared in another module can be constructed with a
module-qualified path — no selective import needed. The qualifier is
explicit naming, not a second construction form: users.User { ... }
builds exactly the same value as User { ... } does after
import users.{ User }, and it picks the right declaration when two
imported modules export the same type name.
-- noexec (multi-file: users.silt declares `pub type User { ... }`)
import users
let alice = users.User { name: "Alice", age: 30, active: true }
Record update syntax creates a new record with fields changed:
let alice2 = alice.{ age: 31 }
The shorthand works here too: alice.{ age } sets age to the value of
the name age.
A keyword may name a field, where only a field name can stand: in the
record type’s body, before : in a literal or an update, in a record
pattern and after a dot. So a JSON document’s "type" key has a field
of its name:
type Event {
type: String,
id: Int,
}
fn main() {
let e = Event { type: "click", id: 1 }
match e {
Event { type: t, .. } -> println(t) -- click
}
println(e.type) -- click
}open in playgroundSuch a field is never written alone (Event { type }): the shorthand
binds or reads a variable, and a keyword names none.
Read as “alice, but with age 31.” Compare to Elm { u | age = 31 }, Rust
User { age: 31, ..u }. Silt’s .{ } syntax avoids new keywords or sigils.
Type Aliases
type Name = T gives the type T another name; the name stands for
T wherever it is written. An alias of a record type builds and
matches that record (type Q = P, then Q { x: 1 } and Q { x } ->),
an alias of an enum writes its variants (type C = Color, then C.Red),
an alias with arguments gives them (type IntBox = Box(Int), then
IntBox { value: 1 } is a Box(Int)), and an alias is a type argument
as its type is (json.parse(text, Q)).
Tuples
Fixed-size, heterogeneous:
let pair = (1, "hello")
let (x, y) = pair
Parentheses around one type or one expression only group: (Int) is Int
and (1) is 1. A tuple of one element is written with a comma, (Int,)
and (1,); () is the unit type and its value.
Recursive Types
Types can reference themselves:
type Expr {
Num(Int),
Add(Expr, Expr),
}
Function Type Annotations
let apply: Fn(Int, Int) -> Int = { a, b -> a + b }
type Handler {
name: String,
run: Fn(String) -> String,
}
Type Ascription
When type inference cannot determine a type from context, use as to assert it:
let x = [] as List(Int)
let r = (int.parse("42") as Result(Int, int.IntError))?
as is a compile-time assertion — if the types conflict, you get a type error.
At runtime it’s a no-op.
A type variable written after as is read as in a let annotation: the
value must have the type whatever the variable stands for. So
({ x -> x } as Fn(a) -> a) is of every type a, and a let of it is
general in a, as let h: Fn(a) -> a = { x -> x } is; ({ x -> x + 1 } as Fn(a) -> a) is refused (type mismatch: expected a, got Int), as that
annotation of it is. A variable of the enclosing function’s signature is
that variable.