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.

TypeHoldsExample
intwhole numbers, 64-bit42, -7, 0xFF
floatdecimal numbers, 64-bit1.5, 0.1, 2.5e-3
bytewhole numbers 0 to 255200, 0xFF
booltrue or falsetrue
strtext"Hello"
erroran error, or nilerror(100, "Disk is full")
filea file, by its pathfile("notes.txt")
jsona JSON value: object, array, string, number, bool or null{"name": "Ann", "age": 30}
date, time, datetimea day, a time of day, or bothdate(2024, 12, 25)
bufferbytes for binary data, read from a positionbuffer([1, 2, 3])
tcpa network connectiontcp.connect("example.com", 80)
handlean open file, read and written at any positionfile("app.db").open()
vecnumbers as 4-byte floats, packed: embeddingsvec([0.1, 0.2])
htmlan HTML document, parsed as browsers parse ithtml.parse("<p>Hi")
processa program running beside this one, its input and outputshell.start(["python3", "tool.py"])
http, http.responsea web request, and its answerhttp("http://localhost:8080/").get()
http.request, http.route, http.serverwhat a web server receives, its routes, the serverhttp.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): Ra function that takes T and gives Rdouble (a function’s name)
?Ta 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 (or 0X).
  • 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:

-9223372036854775808

Division 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.8

Writing floats:

  • Write digits on both sides of the point: 0.5 and 5.0, not .5 or 5..
  • 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, not 1.
  • Very large and very small numbers use exponent form: from 1e+16 up, and below 0.0001.
print(1e15)
print(1e16)
print(0.0001)
print(0.00001)

Output:

1000000000000000.0
1e+16
0.0001
1e-05

Infinity 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
nan

nan 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
MethodWhat 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
  • round and decimals round the digits Luma prints, halves away from zero: 2.675 prints as 2.675, so it rounds to 2.68, as on paper, although the float is really 2.674999… in binary (Python and JavaScript give 2.67 there). 0.5.round() is 1.0, (-0.5).round() is -1.0.
  • sqrt is exact (the nearest float to the root); exp, log, sin, cos and pow are Go’s algorithms (FreeBSD’s and Cephes’), within a bit or two of the exact value, and the same bits in luma run and a built program.
  • All of them give a float, except decimals. For an int, round first: n: int = convert(f.round()) (convert alone cuts toward zero).
  • No result is -0.0 or "-0.00": (-0.001).decimals(2) is "0.00".
  • inf and nan stay as they are: decimals gives "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 = 256 and b: byte = -1 are compile errors.
  • +, - and * wrap around within 0 to 255:
b: byte = 255
b = b + 1
print(b)
b = b - 1
print(b)

Output:

0
255

bool

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
true

str

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:

WriteMeans
\nnew line
\ttab
\rcarriage return, as in the \r\n that ends lines in network protocols
\0the 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 \n for 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 int

To 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 2 can become an int, float or byte.
  • A decimal literal like 2.5 can only be a float.
  • Note the last line of the example: 7 / 2 is 3 with ints, but 3.5 when the result must be a float.