Coming from other languages
If you already know Python, JavaScript or Go, most of Luma will feel familiar. This page shows the same ideas side by side, and the differences worth knowing.
Side by side
| Python | JavaScript | Go | Luma | |
|---|---|---|---|---|
| Variable | x = 5 | let x = 5 | x := 5 | x: int = 5 |
| Constant | PI = 3.14 | const PI = 3.14 | const PI = 3.14 | lock PI: float = 3.14 |
| Function | def add(a, b): | function add(a, b) { | func add(a, b int) int { | fn add(a: int, b: int): int { |
| Short function | lambda x: x * 2 | x => x * 2 | func(x int) int { return x * 2 } | fn(x) => x * 2 |
| Text with values | f"Hi {name}" | `Hi ${name}` | fmt.Sprintf("Hi %s", name) | "Hi ${name}" |
| Loop over a list | for x in xs: | for (const x of xs) { | for _, x := range xs { | xs.walk(x) { |
| With the position | for i, x in enumerate(xs): | xs.forEach((x, i) => { | for i, x := range xs { | xs.walk(i, x) { |
| Count 0 to 9 | for i in range(10): | for (let i = 0; i < 10; i++) { | for i := 0; i < 10; i++ { | (0..9).walk(i) { |
| Leave / skip | break / continue | break / continue | break / continue | stop / skip |
| No value | None | null | nil | nil, only for ?T types |
| Add to a list | xs.append(4) | xs.push(4) | xs = append(xs, 4) | xs[] = 4 |
| Is a key there? | "k" in d | "k" in d | _, ok := d["k"] | has(d, "k") |
| Text to number | int(s) | Number(s) | strconv.Atoi(s) | n: int = convert(s) |
| Own types | class | class | struct | obj |
| Shared methods | protocols | interfaces (TypeScript) | interface | trait |
| Errors | try / except | try / catch | if err != nil | try err { ... }, throw |
| Read input | input("Name: ") | (Node: readline) | bufio.Scanner | prompt("Name: ") |
Coming from Python
- Types are written out.
count: int = 0, and functions name the types of their parameters and result. Once declared, a variable keeps its type. - Blocks use
{ }, not indentation. Indent anyway: it’s easier to read. - Ranges include their end.
(0..9)counts 0 to 9, both included; Python’srange(10)stops before 10. - No
+on strings. Use"${a}${b}"orjoin. - Errors are values.
try err { ... }puts the error inerrinstead of jumping to anexcept. - Programs can be built.
luma buildgives one file you can copy to another computer, with nothing to install there.
Coming from JavaScript
- No
undefined, no truthiness.ifneeds abool: writeif len(name) > 0, notif name. - No implicit conversions.
"5" + 1is an error, not"51". Useconvertwhen you mean it. - Numbers have types.
intfor whole numbers,floatfor fractions, and they don’t mix withoutconvert. - Concurrency without
async. Web servers serve each client in its own coroutine, andwait(list, f)runs a function for every element at the same time. There’s noasyncorawaitto spread through your code; see Several things at once.
Coming from Go
- Names first, types after a colon, and the same everywhere:
x: int,fn f(a: int): str. - No pointers, no
make. Lists, maps and sets are created with literals ([],{}) and shared when assigned. tryinstead ofif err != nil. Onetrycovers a whole block, and errors you don’t catch stop the program with a clear message.- Optional values are checked. A
?strcan’t be used as astruntil you’ve checked it fornil, so there are no nil-pointer crashes. - Memory is freed at once, by reference counting, with no garbage collector pauses.
- One file, no modules to set up.
import "lib/text"imports a file from your project, andluma getfetches mods.