Type Inference

Luma supports type inference to reduce noise in your code. In many cases you don’t need to write the type explicitly - the compiler can infer it from the value on the right-hand side of an assignment.

Basic Inference

x = 42          // inferred as int
y = 3.14        // inferred as float
flag = true     // inferred as bool
name = "Luma"   // inferred as str

You can still write the type explicitly if you want to document intent or enforce stricter rules:

count: int = 42

Both forms are valid - the result is the same.

Collections

When creating lists and maps, types are inferred from the literals:

nums = [1, 2, 3]                  // inferred as [int]
ages = {"Alice": 30, "Bob": 25}   // inferred as {str: int}

If the collection is empty, you need to specify the type, because the compiler has nothing to infer from:

If the collection is empty, you need to specify the type, because the compiler has nothing to infer from:

Optionals

If a variable starts with nil, you must explicitly mark it as optional:

maybe: ?str = nil    // compiler knows this is an optional string

Benefits

  • Cleaner code: less boilerplate when the type is obvious.
  • Safety preserved: inference never bypasses the type system.
  • Works naturally with literals, functions, and most expressions.

Gotcha: Inference only works when the compiler has enough information. If the right-hand side is ambiguous (like [] or {} with no elements), you must provide the type explicitly.

Last updated on