Types
Every value in Luma has a type, and the compiler checks types before the program runs. Types never change silently: an int stays an int.
| Type | Holds | Example |
|---|---|---|
int | whole numbers, 64-bit | 42, -7, 0xFF |
float | decimal numbers, 64-bit | 1.5, 0.1, 2.5e-3 |
byte | whole numbers 0 to 255 | 200, 0xFF |
bool | true or false | true |
str | text | "Hello" |
error | an error, or nil | error(100, "Disk is full") |
file | a file, by its path | file("notes.txt") |
json | a JSON value: object, array, string, number, bool or null | {"name": "Ann", "age": 30} |
date, time, datetime | a day, a time of day, or both | date(2024, 12, 25) |
buffer | bytes for binary data, read from a position | buffer([1, 2, 3]) |
tcp | a network connection | tcp.connect("example.com", 80) |
handle | an open file, read and written at any position | file("app.db").open() |
vec | numbers as 4-byte floats, packed: embeddings | vec([0.1, 0.2]) |
html | an HTML document, parsed as browsers parse it | html.parse("<p>Hi") |
process | a program running beside this one, its input and output | shell.start(["python3", "tool.py"]) |
http, http.response | a web request, and its answer | http("http://localhost:8080/").get() |
http.request, http.route, http.server | what a web server receives, its routes, the server | http.get("/", home) |
[T] | a list of values of type T | [1, 2, 3] |
{K: V} | a map from keys of type K to values of type V | {"a": 1} |
{T} | a set of unique values of type T | {1, 2, 3} |
fn(T): R | a function that takes T and gives R | double (a function’s name) |
?T | a T, or nil (none) | email: ?str = nil |
int
A whole number from −9223372036854775808 to 9223372036854775807 (64 bits).
a: int = 42
b: int = -7
c: int = 0xFF // hexadecimal: 255
print(a)
print(b)
print(c)Output:
42
-7
255- Hexadecimal numbers start with
0x(or0X). - A number literal can’t be larger than 9223372036854775807. The smallest int can only be written as a calculation:
-9223372036854775807 - 1. - When a result goes past the largest or smallest int, it wraps around:
big: int = 9223372036854775807
print(big + 1)Output:
-9223372036854775808Division rules are on the Operators page.
float
A decimal number: a 64-bit IEEE 754 double, as in most languages. It has about 16 significant digits.
f: float = 1.4
g: float = 2.5e3 // 2.5 × 10³
print(f)
print(g)
print(f * 2.0)Output:
1.4
2500.0
2.8Writing floats:
- Write digits on both sides of the point:
0.5and5.0, not.5or5.. - An exponent is written with
e:1.5e3,2e-4,1E+10.
How floats are printed
Luma prints the shortest text that reads back as exactly the same float (when several are as short, the closest to it), the same digits as Python, JavaScript and Go print. Nothing is hidden, so the usual surprises of binary floating point are visible:
a: float = 0.1
b: float = 0.2
print(a + b)
print(a + b == 0.3)Output:
0.30000000000000004
false- A float always shows a decimal point:
1.0, not1. - Very large and very small numbers use exponent form: from
1e+16up, and below0.0001.
print(1e15)
print(1e16)
print(0.0001)
print(0.00001)Output:
1000000000000000.0
1e+16
0.0001
1e-05Infinity and NaN
Float arithmetic follows the IEEE 754 standard. Dividing by zero is not an error for floats: it gives infinity, or “not a number”:
zero: float = 0.0
print(1.0 / zero)
print(-1.0 / zero)
print(zero / zero)Output:
inf
-inf
nannan is not equal to anything, not even itself: nan == nan is false.
Rounding
price: float = 19.999
print(price.round(2))
print("Total: ${price.decimals(2)}")
print(2.675.round(2))
print(2.5.decimals(2))
print(2.7.floor())
print((-2.7).ceil())Output:
20.0
Total: 20.00
2.68
2.50
2.0
-2.0| Method | What it gives |
|---|---|
f.round(n) | the float rounded to n decimals; f.round() is f.round(0), and a negative n rounds to tens, hundreds, …: 1234.5.round(-2) is 1200.0 |
f.decimals(n) | a str with exactly n decimals, for showing a number: 2.5.decimals(2) is "2.50", 3.7.decimals(0) is "4". Never an exponent: 1e20.decimals(2) is "100000000000000000000.00". A negative n is error 11. |
f.floor() | the float rounded down: -2.7 gives -3.0 |
f.ceil() | rounded up: -2.7 gives -2.0 |
f.trunc() | cut toward zero: -2.7 gives -2.0 |
f.sqrt() | the square root: 2.0.sqrt() is 1.4142135623730951 (nan below 0) |
f.exp(), f.log() | e to the power f; the natural logarithm (-inf at 0, nan below) |
f.sin(), f.cos() | of an angle in radians |
f.pow(y) | f to the power y: 2.0.pow(10.0) is 1024.0, 10.0.pow(-2.0) is 0.01 |
roundanddecimalsround the digits Luma prints, halves away from zero:2.675prints as2.675, so it rounds to2.68, as on paper, although the float is really 2.674999… in binary (Python and JavaScript give 2.67 there).0.5.round()is1.0,(-0.5).round()is-1.0.sqrtis exact (the nearest float to the root);exp,log,sin,cosandpoware Go’s algorithms (FreeBSD’s and Cephes’), within a bit or two of the exact value, and the same bits inluma runand a built program.- All of them give a float, except
decimals. For anint, round first:n: int = convert(f.round())(convertalone cuts toward zero). - No result is
-0.0or"-0.00":(-0.001).decimals(2)is"0.00". infandnanstay as they are:decimalsgives"inf","-inf"or"nan".
byte
A whole number from 0 to 255: one byte of memory.
b: byte = 200
c: byte = 0xFF
print(b)
print(c)Output:
200
255- A literal must fit:
b: byte = 256andb: byte = -1are compile errors. +,-and*wrap around within 0 to 255:
b: byte = 255
b = b + 1
print(b)
b = b - 1
print(b)Output:
0
255bool
true or false. Comparisons such as x > 5 produce a bool, and if needs one.
ready: bool = true
done: bool = 3 > 5
print(ready)
print(done)
print(!done)Output:
true
false
truestr
Text in double quotes. A string can contain any Unicode text.
s: str = "Tere, maailm! ÕÄÖÜ"
print(s)
print("Tab:\tend")
print("She said \"hi\"")Output:
Tere, maailm! ÕÄÖÜ
Tab: end
She said "hi"Strings have methods for case, trimming, searching, splitting and replacing: see Working with text.
Escape sequences:
| Write | Means |
|---|---|
\n | new line |
\t | tab |
\r | carriage return, as in the \r\n that ends lines in network protocols |
\0 | the byte 0 |
\" | a double quote |
\\ | a backslash |
\$ | a dollar sign (to write ${ without interpolating) |
- A string must end on the same line it starts on. Use
\nfor line breaks. - Strings can be compared with
==and!=. ${...}inside a string inserts the value of an expression:"Hello ${name}". That is also how strings are joined:"${a}${b}". See Interpolated strings.
Types don’t mix
Luma never converts between types automatically. An int and a float (or a byte) can’t be combined:
i: int = 2
f: float = 1.5
print(f * i) // error: operator * needs both sides to have the same type, got float and intTo convert on purpose, use convert: f: float = convert(i).
Number literals adapt
A number written directly in the code takes the type it is used with, as long as the value fits:
f: float = 1 // 1 becomes 1.0
b: byte = 200
b = b + 1 // 1 is a byte here
x: float = 7 / 2 // both 7 and 2 become floats
print(f)
print(b)
print(x)Output:
1.0
201
3.5- A whole-number literal like
2can become anint,floatorbyte. - A decimal literal like
2.5can only be afloat. - Note the last line of the example:
7 / 2is3with ints, but3.5when the result must be a float.