From 0.1.0 to 0.2.0: What Changed in the Language
Earlier today we announced that Luma’s own compiler works. That post was about what happens under the hood. This one is about what you see when you write code.
Rebuilding Luma from scratch gave us a rare chance: to keep everything we liked about 0.1.0 and fix the things that had grown awkward. Luma 0.2.0 is still clearly Luma, with the same name: type = value variables, lists, maps and sets, walk loops with skip and stop, interpolated strings and lock. But several parts of the language are now simpler, more consistent and safer.
Here are the major changes. Not every detail, just the ones you’ll notice first.
Conversion: one word instead of dozens
In 0.1.0, every conversion had its own method: to_int(), to_float(), to_str(), to_bool(), to_byte(), and many more, each with a version that gave nil and another that took a default.
In 0.2.0 there is one word: convert. The type you convert to is simply the type of the place the value goes into:
text: str = "42"
n: int = convert(text)
price: float = convert("9.99")
label: str = convert(n)Your code already says what you want: n: int = .... Writing to_int() too was saying it twice. And if nothing tells Luma what to convert to, it’s a compile error that explains how to fix it. Luma still never converts anything on its own.
Functions: cleaner and shorter
The result type is now written just like a variable’s type, after a colon. The old arrow is gone:
// 0.1.0
fn greet(name: str) -> str {
return "Hello, ${name}!"
}
// 0.2.0
fn greet(name: str): str {
return "Hello, ${name}!"
}One rule for everything: a variable is name: type, a parameter is name: type, and a function is name(...): type.
Short functions fit on one line with =>, and parameters can have default values:
fn add(a: int, b: int): int => a + b
fn welcome(name: str, greeting: str = "Hello"): str => "${greeting}, ${name}!"Lambdas look like functions too, just without a name, instead of having a syntax of their own:
// 0.1.0
nums.filter(x -> x % 2 == 0)
// 0.2.0
nums.filter(fn(n) => n % 2 == 0)Since -> is gone from functions and lambdas, walk loops lost it too. The block simply follows, just like after if:
nums.walk(n) {
print(n)
}Error handling: clear and complete
0.1.0 had four tools for errors: error("...") to raise, .or() to handle, try to pass an error on, and panic to crash. They worked, but it wasn’t always obvious which one to use, or where an error would end up.
0.2.0 makes it one simple idea. Code that may fail goes in a try block, and the error, if any, lands in a variable you name:
err: error = nil
try err {
age: int = convert("abc")
}
if err != nil {
print("Not a number: ${err.message}")
}Your own code raises errors with throw:
fn check_age(age: int) {
if age < 0 {
throw error(100, "age can't be negative")
}
}What’s better:
- Every error has a code and a message.
err.codetells your program what went wrong;err.messagetells people. Luma’s own errors have fixed codes (division by zero, a missing map key, a file that can’t be read…), so you can react to each one. - It’s always visible. An error is either caught by a
tryyou can see, or it stops the program with a message that names the file, line and column. - Nothing is left behind. When an error jumps out of your code, everything it was holding is cleaned up.
Structs and impl blocks become obj
In 0.1.0, data and behaviour lived apart: a struct held the fields, and a separate impl block held the methods. In 0.2.0 they live together in an obj:
obj Person {
name: str
age: int = 0
fn greet(): str => "Hi, I'm ${self.name}"
fn birthday() {
self.age = self.age + 1
}
}
ann: Person = Person { name: "Ann", age: 30 }
ann.birthday()
print(ann) // Person {name: "Ann", age: 31}Fields can have default values, and print shows the whole object. Traits still work as before: an obj lists the traits it implements, and code can work with any object that has them.
Optional values: safer by design
?str, ?int and the other optional types are still here, but now the compiler guarantees that you never use a missing value by accident. Before you can use a ?str as a str, you either check it:
if email != nil {
print(email.upper()) // here email is a str
}or give a default with ??:
print(email ?? "no email")Forget both, and it’s a compile error that tells you exactly these two options. A program can’t crash because a value was missing.
New: testing is built in
This one is completely new. Luma code can now be tested, with nothing to install.
Tests go in a file next to your code, named with _test. A test is a function whose name starts with test_, and assert checks that something is true:
// hello.luma
fn hello(name: str): str => "Hello, ${name}!"
// hello_test.luma
fn test_hello() {
assert hello("Ann") == "Hello, Ann!"
}
fn test_empty_name() {
assert hello("") == "Hello!"
}Run them with luma test:
hello_test.luma
PASS test_hello
FAIL test_empty_name
hello_test.luma:6:5: assert failed: hello("") == "Hello!"
got: "Hello, !"
want: "Hello!"
2 tests, 1 failedWhen a check fails, Luma shows the line, the condition as you wrote it, and both sides: what you got and what you want. luma test ends with an error code when a test fails, so it can stop a build or a deploy.
And because Luma runs in two ways, tests can too: luma test uses the interpreter for quick feedback, and luma test -build runs the same tests compiled.
What stayed the same
Most of Luma did. Variables, lock, lists, maps and sets, interpolated strings, walk with skip and stop, match, modules with import and pub, traits: all still here, and all now checked and compiled by the new compiler. If you wrote Luma 0.1.0, 0.2.0 will feel familiar, just tidier.
What’s next
The language is in place and the compiler is running. Next we’ll write about the standard library: files, JSON, dates, HTTP and the rest, and what it means to have them built into a language that makes its own programs.