Language reference · Section 3
Types
Plumlang is statically typed: every expression has a type known before the program runs, and type mistakes are compile errors. Most types are inferred, so they rarely need writing.
3.1 Basic types
| Type | Values | Notes |
|---|---|---|
int |
64-bit signed integers | Overflow is a runtime error, never wrap-around |
float |
64-bit IEEE 754 numbers | |
bool |
good, bad |
|
str |
immutable UTF-8 text | |
byte |
integers 0 to 255 | For binary data, in list[byte] |
none |
the single value none |
The type of functions that return nothing |
3.2 Conversions
There is exactly one implicit conversion: an int becomes a float
where a float is expected, including in arithmetic that mixes them.
plant x: float = 5 # 5.0
plant y = 2 * 1.5 # 3.0, a floatEvery other conversion is written:
| Conversion | Result |
|---|---|
int(3.9) |
3 (truncates toward zero; error if out of range or NaN) |
float(3) |
3.0 |
str(42), str(1.5), str(good) |
"42", "1.5", "good" |
int("42"), float("1.5") |
parse, or throw a ParseError |
bool(x) |
not allowed: write the comparison you mean |
There is no truthiness: if count { is a type error; write if count > 0 {.
3.2.1 How values print
str(), drop and interpolation turn values into text the same way:
| Value | Text |
|---|---|
int |
decimal digits: 42, -7 |
float |
the shortest text that reads back as the same number, always with a . or an exponent. Exponents from −4 to 15 are written out (1.0, 0.0025, 1000000000.0), others in scientific notation (1e+20, 1e-05); also inf, -inf, nan |
bool |
good or bad |
str |
the text itself, without quotes |
none |
none |
| list, map, set | [1, 2], {"a": 1}, set([1, 2]), with strings inside quoted |
| tree | its to_str() (trait Show), else <TreeName> |
Values nested more than 256 deep (a long chain of kind values, say) print
their outer 256 levels and ... for what is inside, so printing never
runs out of stack. The depth counts through to_str() methods that print
values of their own.
3.3 Optional types
T? is either a T or none. Only optional types can hold none, so a
plain str is never absent, and nothing crashes on a missing value.
plant nickname: str? = none
drop(nickname ?? "no nickname") # default when none
if plant n = nickname { # unwrap
drop("Called {}") # n is a str here
}
drop(nickname?.upper()) # none-safe call: a str?3.4 Collections
| Type | Literal | Notes |
|---|---|---|
list[T] |
[1, 2, 3], [] |
Ordered, growable. Index from 0 |
map[K, V] |
{"a": 1}, {:} |
Keys are int, str, bool, byte or a kind; insertion order is kept |
set[T] |
set([1, 2]) |
Unique values, insertion order kept |
The empty literals [] and {:} need a type from context:
plant names: list[str] = []
plant ages: map[str, int] = {:}Reading a list out of bounds, or a missing map key with m[k], is a
runtime error; m.get(k) returns V? instead.
3.5 Function types
(int, int) -> int is the type of a function taking two ints and
returning an int. () -> none takes nothing and returns nothing.
3.6 User-defined types
tree declares a class (section 8), trait an interface (section 8.5),
and kind an enumeration (section 8.6).
3.7 Type annotations
Annotations follow a name after :. They are needed on function
parameters, function results other than none, tree fields, and where
inference has nothing to go on (empty collections, none).
plant count = 0 # int, inferred
plant ratio: float = 1 # float
plant scores: list[int] = []