Common errors

Every Luma error names the file, the line and the column, and says what’s wrong in everyday words. Most are found before the program runs. Here are the ones you’ll meet first. The full list is in Errors.

Errors when compiling

Mixing types

mix.luma:3:9: operator + needs both sides to have the same type, got int and float

Luma never converts a value by itself, so int + float isn’t allowed. Convert one side first:

a: int = 5
b: float = 2.5
total: float = convert(a)
print(total + b)

Assigning the wrong type

assign.luma:1:14: cannot assign str to count, which has type int

count: int = "5" puts text into an int. Write the number (5), or convert the text: count: int = convert("5").

Using a variable that doesn’t exist

undecl.luma:1:7: undefined variable total
nodecl.luma:1:1: undefined variable x (declare it first: x: type = value)

Every variable is declared once, with its type and a value: total: int = 0. After that, assign without the type: total = total + 1. Check the spelling, and check that the variable is declared in a block that the code can see; see Variables.

Declaring a variable twice

twice.luma:2:1: x is already declared at 1:1

The second time, leave out the type: x = 2 assigns, x: int = 2 declares.

A value that may be missing

opt.luma:2:7: email is a ?str and may be nil: check it first (if email != nil { ... }), or give a default (email ?? ...)

An optional value can’t be used until you know it’s there. Do what the message says:

email: ?str = nil
if email != nil {
    print(email.upper())
}
print(email ?? "no email")

See Optional values.

nil for a type that can’t be missing

nilint.luma:1:10: nil is for optional values and errors: email: ?str = nil, err: error = nil

Only optional types (?int, ?str, …) and error can be nil. Use x: ?int = nil if the value may really be missing.

Converting without saying to what

conv.luma:1:7: nothing says what to convert to here; put the result into a variable of the type you want, e.g. n: int = convert(text)

convert takes its target type from the place the value goes. print(convert("5")) doesn’t say, so put the result in a typed variable first.

Joining strings with +

plus.luma:2:9: operator + does not work on strings

Use interpolation: "${a}y", or join for a list of strings.

Comparing lists

streq.luma:3:9: operator == does not work on lists

Compare the elements one by one. Limitations shows a way for sets.

A function that can end without a result

ret.luma:1:4: f must return an int, but it can reach its end without a return

Every way through a function with a result type must end in return. Check the else branches.

Calls

args.luma:2:7: f expects 1 argument, got 2
unk.luma:1:7: unknown function "foo"

Check the function’s parameters, and its spelling. A function from another file is called through its import: text.upper(s).

Changing a locked value

lockch.luma:2:1: PI is locked (at 1:6) and can't be changed

lock makes a value fixed when the program is compiled. Use an ordinary variable if it needs to change.

Block comments

comment.luma:1:1: Luma has no /* */ comments; start each comment line with //

Errors while running

These stop the program, unless they happen inside a try; see Error handling.

div.luma:3:9: runtime error: division by zero
key.luma:2:11: runtime error: key "Bo" not found
idx.luma:2:11: runtime error: index 5 out of range (length 2)
badconv.luma:1:10: runtime error: cannot convert "abc" to an int
  • Division by zero: check the divisor first.
  • A missing map key: check with has(ages, "Bo") before reading it.
  • An index out of range: check len(nums), or walk the list instead of counting.
  • A failed conversion: text from a user or a file may not be a number. Convert it inside try:
err: error = nil
try err {
    n: int = convert("abc")
    print(n)
}
if err != nil {
    print("Not a number: ${err.message}")
}