Gotchas

Variables and Primitive Types

Working with primitive types in Luma is straightforward, but there are a few things to keep in mind. These aren’t bugs - just design choices that can surprise you if you’re coming from other languages.

int vs float

  • Luma does not silently mix int and float.
  • If you add them together, the compiler enforces explicit or implicit coercion.
a: int = 5
b: float = 2.5

// result must be float
avg: float = (a + b) / 2   // `a` is coerced to float

This is by design: you always know what type the result will be.

byte is not str

  • A byte is a single numeric value (0–255).
  • A str is a sequence of characters.
  • Assigning one to the other doesn’t work directly.
b: byte = 65
text: str = "A"

// This won’t work:
// text = b
// because `b` is a number, not a string.

If you want to display a byte as text, use string interpolation:

print("Byte as char: ${b}")

nil and optional types

  • Only optional types (?int, ?str, etc.) can be set to nil.
  • Regular int, str, bool, etc. must always have a value.
maybe: ?int = nil   // allowed
count: int = nil    // error

Boolean logic

  • &&, ||, and ! only work with bool.
  • They do not auto-convert numbers (0, 1) to boolean like some languages do.
flag: bool = true
if flag && !false {
    print("ok")
}

Gotchas with Optionals

Optional types are powerful, but they come with a few important caveats:

  • Only optionals can be nil
count: int = nil    // not allowed
maybe: ?int = nil   // allowed
  • Comparisons require care
    If you compare an optional to a non-optional value, the optional must not be nil.
x: ?int = nil
if x == 5 {          // ❌ unsafe if x is nil
    print("five")
}
  • Operations on nil optionals will fail
    Arithmetic and string operations assume a value is present. Always check for nil first, or use conversion with defaults.
s: str = "abc"
n: int = s.to_int(0)    // returns default 0 if conversion fails
  • Printing optionals shows nil literally
    This is intentional for clarity, but may surprise newcomers.
x: ?str = nil
print(x)    // prints "nil"

More Gotchas Coming…

This page is open for more tips and common pitfalls. As Luma evolves, we’ll extend this section with clarifications around variables, types, and expressions.

Last updated on