Coming from Python or JavaScript
If you already know Python or JavaScript, most of Cresset will feel familiar, and the parts that are different are listed here. Cresset's error messages also recognize most of the habits below and tell you what to write instead.
The same idea, written differently
| In Python or JavaScript | In Cresset | ||
|---|---|---|---|
def f(x): / function f(x) { | fn f(x): | ||
None, null, undefined | none | ||
True, False | true, false | ||
&&, `\ | \ | , !x` | and, or, not x |
x++, x-- | x += 1, x -= 1 | ||
else if / elseif | elif | ||
let x = 1, const x = 1, var x = 1 | x = 1 (no keyword) | ||
// comment | # comment | ||
lambda x: x * 2 / x => x * 2 | x -> x * 2 | ||
f"hello {name}" / ` hello ${name} ` | "hello {name}" | ||
class Dog: | record Dog: with fields, and methods inside it | ||
import math / require("fs") | use name from "file.crs" | ||
try / except / catch | a function returns ok(...) or err(...), and ? passes a failure up | ||
switch | match | ||
[x * 2 for x in xs] | xs.map(x -> x * 2) | ||
{"a": 1} | {"a": 1} (the same) | ||
for i in range(3): | for i in range(3): (the same) | ||
print(x) | print(x), in a function that says needs print |
The big difference: permissions
In Python or JavaScript any function can read a file or open a network connection. In Cresset a function must say so in its signature, and the program must be allowed to do it:
fn word_count(text):
return text.split().len()
fn main() needs print:
print(word_count("one two three"))
word_count declares nothing, so it can't touch anything outside itself. main says needs print, so it can print. A function that tries to read a file without saying needs read is refused before the program runs. See Effects and permissions.
Small things that catch people out
Text. { starts an insertion inside double quotes, so a literal brace is written \{. Single quotes are taken exactly as written (no insertions, no escapes), which is handy for JSON: json_parse('{"a": 1}').
Functions can't change outside variables. A function can read a variable defined at the top of the file, but assigning inside a function always makes a new local. There is no global or nonlocal; return the new value instead.
No nested functions. Define functions at the top level, and use short x -> ... arrows for small inline ones.
Comparisons don't chain. 1 < x < 10 is an error; write 1 < x and x < 10.
Several return values are a list: return a, b and a, b = f() work like a tuple.
Mistakes become values, not exceptions. to_int("abc") gives back an err(...) rather than raising, and ? passes it up to the caller:
fn double_text(text):
n = to_int(text)?
return ok(n * 2)
fn main() needs print:
print(double_text("21"))
print(double_text("x"))
ok(42)
err("'x' is not a whole number")
Lists and maps are shared. Like Python and JavaScript, two names for one list see the same changes. Use list.slice(0) or xs[:] for a copy.
Counting from zero, negative indexes from the end: xs[-1] is the last item, as in Python.
Division is real division: 7 / 2 is 3.5. Use floor(7 / 2) for 3.
Next
The language tour covers everything above in more detail.