Coming from other languages

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

PythonJavaScriptGoLuma
Variablex = 5let x = 5x := 5x: int = 5
ConstantPI = 3.14const PI = 3.14const PI = 3.14lock PI: float = 3.14
Functiondef add(a, b):function add(a, b) {func add(a, b int) int {fn add(a: int, b: int): int {
Short functionlambda x: x * 2x => x * 2func(x int) int { return x * 2 }fn(x) => x * 2
Text with valuesf"Hi {name}"`Hi ${name}`fmt.Sprintf("Hi %s", name)"Hi ${name}"
Loop over a listfor x in xs:for (const x of xs) {for _, x := range xs {xs.walk(x) {
With the positionfor i, x in enumerate(xs):xs.forEach((x, i) => {for i, x := range xs {xs.walk(i, x) {
Count 0 to 9for i in range(10):for (let i = 0; i < 10; i++) {for i := 0; i < 10; i++ {(0..9).walk(i) {
Leave / skipbreak / continuebreak / continuebreak / continuestop / skip
No valueNonenullnilnil, only for ?T types
Add to a listxs.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 numberint(s)Number(s)strconv.Atoi(s)n: int = convert(s)
Own typesclassclassstructobj
Shared methodsprotocolsinterfaces (TypeScript)interfacetrait
Errorstry / excepttry / catchif err != niltry err { ... }, throw
Read inputinput("Name: ")(Node: readline)bufio.Scannerprompt("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’s range(10) stops before 10.
  • No + on strings. Use "${a}${b}" or join.
  • Errors are values. try err { ... } puts the error in err instead of jumping to an except.
  • Programs can be built. luma build gives one file you can copy to another computer, with nothing to install there.

Coming from JavaScript

  • No undefined, no truthiness. if needs a bool: write if len(name) > 0, not if name.
  • No implicit conversions. "5" + 1 is an error, not "51". Use convert when you mean it.
  • Numbers have types. int for whole numbers, float for fractions, and they don’t mix without convert.
  • Concurrency without async. Web servers serve each client in its own coroutine, and wait(list, f) runs a function for every element at the same time. There’s no async or await to 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.
  • try instead of if err != nil. One try covers a whole block, and errors you don’t catch stop the program with a clear message.
  • Optional values are checked. A ?str can’t be used as a str until you’ve checked it for nil, 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, and luma get fetches mods.