Salam for Python, Go and JavaScript programmers: the traps
Salam looks like Python with end instead of indentation, but nine details behave differently from what Python, Go and JavaScript programmers expect: until is while, else cond is else if, switch falls through, integers are i32, strings are heap values you free, and more. Each trap has a program and its output.
Most of Salam can be read on the first day by someone who knows Python, Go or JavaScript. Blocks open with a colon and close with end, println prints its arguments separated by spaces, := declares a variable, and each x in xs loops over a collection. The danger is not the syntax you cannot read; it is the syntax you read wrongly. This guide lists the places where the familiar meaning is the wrong one. Every program below was run, and the output shown is what it printed.
What carries over
You know
In Salam
Python for x in xs:
each x in xs: ... end
Python for i in range(n):
repeat n in i: ... end
Python elif
else cond:
Go x := 1
x := 1 (immutable; use mut x := 1 to reassign)
Go defer f.Close()
defer f.Close()
Go for { ... }
until true: with break
JavaScript const x = 1
x := 1
JavaScript let x = 1
mut x := 1
JavaScript x => x * 2
(x: i32): ret x * 2 end
Exceptions or error returns
throw, try f() and try: ... catch e: ... end
There are no braces, no semicolons (a ; is an error), no while and no for. The language reference has the full grammar; the rest of this page is about behaviour.
1. until is while
until reads like "until the condition is true", and it does the opposite: it repeats while the condition is true. There is no while keyword, so until is the only conditional loop. Copy the condition from a while loop unchanged.
funcmain:
mut i := 0until i < 3:
println i
i++
endend
output
0
1
2
Writing until i >= 3: here, the way the English word suggests, runs the body zero times and prints nothing, with no error. When a loop does nothing, check the polarity of its condition first.
2. else cond is else if
A chain of conditions uses else followed by the next condition. There is no elif (the parser also tolerates else if), and the whole chain has one end. A plain else: with a colon right after it is the final branch.
funcgrade(score: i32): str:
if score >= 90:
ret"A"else score >= 80:
ret"B"else:
ret"C"endendfuncmain:
printlngrade(95), grade(85), grade(40)
end
output
A B C
elif is an error that tells you to write else.
3. switch falls through
A switch is not a match and not a Python match statement. Each label is a value, a list, a range (2 to 4) or a relational test (> 10), and the body of a label continues into the next label unless it ends with break (or ret). Every label has its own end, and else must be last.
funcmain:
n := 2switch n:
1:
println"one"end2:
println"two"end3:
println"three"breakendelse:
println"other"endendend
output
two
three
n is 2, so the program printed two, fell into the label for 3 and printed three, and stopped at that break. Go programmers will find this the reverse of Go, where each case ends by itself. If you want no fallthrough and a compiler that checks every enum member is handled, use match instead.
4. Integers are i32 unless you say otherwise
An integer literal is an i32 when it fits and an i64 when it does not. A variable made from it keeps that type, and arithmetic stays in that width. When the compiler can see the product overflow, it refuses (error[E148]: integer overflow: 'a * a' gives 10000000000, which does not fit in 'i32'). Cast one operand to a wider type first with as. A narrower type does not convert back for free: passing an i64 where an i32 is expected is error[E012]: argument 1: cannot pass 'i64' to 'i32'.
funcmain:
small := 100000
wide := small asi64println wide * wide
println7 / 2, 7.0 / 2.0end
output
10000000000
3 3.5
The second line shows two more differences. Integer / truncates, so 7 / 2 is 3, and / never turns integers into floats the way JavaScript or Python 3 would. Put the cast in parentheses when you cast and then multiply: (a as i64) * (b as i64), because after as i64 a * reads as the start of a pointer type.
5. A function that returns a string returns memory you free
str has no garbage collector behind it. A function that returns a str hands you a freshly allocated string, and you own it: free it with mem.Free when you are done. Strings that you only read through (literals, and arguments you were given) are not yours to free. This feels wrong after Python or JavaScript, where strings just disappear when nothing refers to them. In a short program the leak is harmless; in a server it adds up. Vectors, hash maps and structs that hold strings follow the same rule, usually with defer x.Free() right after you create them.
import mem
importstrfuncmain:
label := str.FromInt(42asi64)
println"id:", label
mem.Free(label asvoid*)
end
output
id: 42
Strings are also bytes, not UTF-16 or code points: the length of a Persian word is its byte count, and there is a separate method to count letters.
6. Structs copy, collections share
Assigning a struct copies it, as in Go: change the copy and the original is untouched. A Vector or HashMap is a handle to a buffer, and assigning it moves or shares that buffer, as with a Python list or a JavaScript array. The compiler guards the common mistake: reading the old name after the buffer may have moved is E259 or E258. If you want two independent collections, ask for a copy with .Copy().
structPoint:
pub x: i32 = 0endfuncmain:
p := Point { x = 1 }
mut q := p
q.x = 9println p.x, q.x
list := Vector {} asVector<i32>
defer list.Free()
list.Push(1)
more := list.Copy()
defer more.Free()
more.Push(2)
println list.Len(), more.Len()
end
output
1 9
1 2
p stays 1 while the copy q becomes 9, and the vector list keeps its one element while its copy more gets a second.
7. Unused things are errors
An unused variable, parameter, import or private function stops the build. This surprises everyone once. A variable declared with mut that is never reassigned is an error too. Remove what you do not use; a name that starts with _ silences the check, but it leaves dead code behind, so keep that for a parameter in a fixed callback signature.
The compiler says error[E059]: unused variable 'unused': nothing uses it after it is declared, with a fix that prefixes the name with _.
8. The order of a file is fixed
Go and Python let you define things in any order. Salam checks the order within a file: package, then import, then include, then global variables and constants, then types (struct, enum, type, interface, impl), then private functions, then pub functions. A struct written after the first function is E087, a global after a type is E085, and a private function after a pub one is E088. Functions may call each other in any order within that frame, so only the grouping matters. main is exempt from the pub rule.
9. Lambdas capture by value
A lambda is written (x: i32): ret x * 2 end, with no func and no return type, and it copies the variables it uses at the moment it is created. A JavaScript closure sees later changes; a Salam lambda does not.
funcmain:
mut n := 1
show := (): ret n end
n = 5printlnshow(), n
end
output
1 5
show still returns 1 after n became 5. To share state with a lambda, put it behind a pointer, a collection or a global.
Where to go next
The Salam vs Python article compares the two languages on speed and ecosystem, the static typing article covers the type system, and the /learn/ course walks through the language from the start.
Try Salam yourself
Salam is free and open source. Run the examples from this article in the online editor, follow the course from the first lesson, or install the compiler with one command.