Type Introspection: x.type(), x.is_type("int")

Type Introspection: x.type(), x.is_type("int")

Luma provides built-in type reflection methods so you can inspect or validate variable types at runtime in a clean, safe, and expressive way.

Overview

Sometimes you need to know what type a value has - especially when working with dynamic or optional data. Luma offers two introspection methods:

  • x.type() → returns the type name as a string
  • x.is_type("typeName") → returns a boolean indicating if the value matches that type

Examples

s: str = "hello"
i: int = 42
f: float = 3.14
b: bool = true

print(s.type())        // "str"
print(i.type())        // "int"
print(f.type())        // "float"
print(b.type())        // "bool"

print(i.is_type("int"))     // true
print(s.is_type("float"))   // false

How It Works

These methods are automatically transformed at compile time into safe built-in Go functions:

Luma Compiles to Go
x.type() _luma_type(x)
x.is_type(“int”) _luma_is_type(x, “int”)

These helpers avoid name collisions and are internal to the compiler.

Supported Types

The following type names can be checked:

  • “str”
  • “int”
  • “float”
  • “bool”
  • “byte”
  • “list” (for T)
  • “map” (for {K: V})
  • “set” (for T)
  • “nil” (for nil values)

Example:

list: [int] = [1, 2, 3]
print(list.type())       // "list"
print(nil.is_type("nil")) // true

Type Names Are Strings

Be sure to use lowercase string names. They are not type keywords:

x: int = 5
print(x.is_type("int"))    // ✅ true
print(x.is_type(int))      // ❌ invalid: must use "int"

Best Practices

  • Use is_type() when branching logic depends on type.
  • Use .type() for debugging, logging, or dynamic behavior.
  • Don’t overuse introspection - rely on strong typing where possible.
Last updated on