Pub Values: How Luma Learned to Export More Than Functions

Pub Values: How Luma Learned to Export More Than Functions

February 16, 2026·
Luma Core Team

We kept running into the same wall. You’d write a module that connects to a database, sets up a configuration, or computes something expensive – and then you had no good way to hand that result to the rest of your program. You could wrap it in a function and call it every time, but that either reconnects on every call or forces you to build a caching pattern that the language should handle for you.

This week we fixed it. Luma now supports pub name = expr – exporting a value directly from a module, with the compiler inferring the type automatically.

The problem we kept hitting

Here’s what database connection code looked like before:

// db/connection.luma
mysql = import "../modules/mysql"

pub fn connect() -> any {
    return db.open(mysql, "root:@localhost:3306/myapp")
}

The return type has to be any because the parser didn’t accept pub conn = expr and the compiler couldn’t carry inferred types through module boundaries. Every caller had to do:

database = import "db/connection"
conn = database.connect()

And that conn was typed as any internally, which meant no method dispatch. conn.query(...) wouldn’t work because the compiler didn’t know conn was a *LumaDbConn. You’d have to fight the type system instead of writing your application.

What changed

You can now write:

// db/connection.luma
mysql = import "../modules/mysql"

pub conn = db.open(mysql, "root:@localhost:3306/myapp")

And from any other module:

database = import "db/connection"

rows = database.conn.query("SELECT * FROM users")
rows.walk(row) -> {
    print(row.get("name"))
}

The compiler:

  1. Parses pub name = expr without requiring a type annotation
  2. Infers the type from the value expression – db.open() returns *LumaDbConn, so conn is *LumaDbConn
  3. Emits the variable at Go package level so it’s initialized once, at program startup
  4. Carries the inferred type through the module namespace struct, so database.conn has the correct type for method dispatch

No any. No wrapper functions. No ceremony. The connection is created once when the module loads and available everywhere it’s imported.

Not just for databases

The pattern works for any value you want to compute once and export:

// config.luma
pub app_name = "My Application"
pub version = 42
pub pi = 3.14159

pub fn describe() -> str {
    return "This is ${app_name} v${version}"
}
// main.luma
cfg = import "config"

print(cfg.app_name)    // "My Application"
print(cfg.version)     // 42
print(cfg.pi)          // 3.14159
print(cfg.describe())  // "This is My Application v42"

Values and functions coexist naturally in the module namespace. The importer accesses both the same way – through the module object.

What the compiler does under the hood

When you write pub conn = db.open(mysql, "root:@localhost:3306/myapp") in connection.luma, several things happen at compile time:

Parsing. The parser now accepts pub IDENT = expr alongside the existing pub name: Type = expr. The type annotation is optional – if you omit it, the compiler infers it.

Type inference. In the module export indexer, the compiler calls guessType() on the value expression. For db.open(...) it returns *LumaDbConn. For literals like 42 it returns int, for "hello" it returns string. The inferred type becomes the struct field type in the module namespace.

Package-level emission. Pub VarDecls are emitted at Go package level, not inside main(). This means they’re initialized once at program startup, before any function runs. The database connection opens once, the config values are set once, and they stay available for the lifetime of the program.

Namespace struct wiring. The module namespace struct gets a properly typed field:

// database = import "db/connection"
var database = struct{ conn *LumaDbConn }{ conn: connection_conn }

Because the field type is *LumaDbConn (not interface{}), method calls like database.conn.query(...) dispatch correctly without type assertions.

Three changes, zero new syntax

We modified the parser to accept an optional type annotation. We modified the compiler to infer types for untyped pub exports and emit them at package level. We added module field type tracking so method dispatch works through module boundaries.

No new keywords. No new concepts. pub already meant “export this.” Now it exports values as naturally as it exports functions. If you’ve been wrapping values in getter functions just to export them, you can stop.

One detail worth knowing

When you write pub conn = db.open(mysql, "root:@localhost:3306/myapp"), the connection opens at module load time – the moment your program starts. This is exactly like Go’s package-level var db = sql.Open(...) pattern. If the database isn’t running, your program panics at startup rather than at the point of first use.

For most applications this is the right behavior. You want to know immediately if your infrastructure is unreachable, not discover it when the first request arrives. If you need lazy initialization, you can still use the function pattern:

pub fn connect() -> any {
    return db.open(mysql, "root:@localhost:3306/myapp")
}

Both patterns are valid. pub name = expr gives you eager initialization with type safety. A pub fn gives you lazy evaluation with explicit invocation.

Looking forward

This was a small change to the parser – about 10 lines. The compiler work was more involved: type inference through module boundaries, package-level emission, namespace field type tracking. But the result feels obvious in hindsight. A module system that can export functions and structs but not values was always going to frustrate people building real applications.

The connection pattern was the motivating case, but we expect pub name = expr to show up in configuration modules, precomputed lookup tables, shared constants, and anywhere else you want to evaluate something once and share it across your codebase.

Last updated on