Zolo Cheatsheet
Variables #
let x = 10
let mut y = 0
const PI = 3.14
Types #
int f64 str bool nil
[int ]
{str : int }
(int , str )
int ?
fn (int ) -> int
Map <str , int >
Functions #
fn add(a: int , b: int ) -> int { a + b }
let double = |x| x * 2
fn apply(f: fn (int ) -> int , x: int ) -> int { f(x) }
Control Flow #
if cond { } else if cond2 { } else { }
let x = if cond { a } else { b }
if let Some (v) = opt { use (v) }
match val { 0 => "zero", _ => "other" }
Loops #
for i in 0 ..10 { }
for i in 0 ..=10 { }
for item in list { }
for (i, v) in pairs { }
while cond { }
while let Some (x) = iter() { }
loop { break }
Structs #
struct Point { x: f64, y: f64 }
let p = Point { x: 1.0 , y: 2.0 }
impl Point { fn dist(self ) -> f64 { ... } }
Enums #
enum Color { Red, Green, Blue }
enum Shape { Circle(f64), Rect(f64, f64) }
let s = Shape::Circle(5.0 )
Traits #
trait Display { fn show(self ) -> str }
impl Display for Point { fn show(self ) -> str { "..." } }
Pattern Matching #
match value {
0 => "zero",
n if n < 0 => "negative",
Shape::Circle(r) => "circle {r}",
Point { x, y } => "({x},{y})",
39 ;a39 ; | 39 ;b39 ; => "ab",
_ => "other",
}
Operators #
Strings #
"Hello, {name}!"
"""multiline"""
sql"SELECT * FROM t WHERE id={x}"
Decorators #
@test fn test_it() { ... }
@memoize fn fib(n) { ... }
@deprecated ("msg") fn old() { }
@builder struct Config { ... }
Standard Library #
string.trim(s) string.split(s, sep) string.contains(s, sub)
Array .map(a, f) Array .filter(a, f) Array .reduce(a, f, init)
Array .push(a, v) Array .len (a) Array .sort(a)
Map .new() Map .set(m, k, v) Map .get(m, k) Map .keys(m)
Set .from (arr) Set .add(s, v) Set .has(s, v) Set .union(a, b)
Option .Some (v) Option .None () Option .unwrap_or(o, def)
Result .Ok (v) Result .Err (e) Result .unwrap(r)
Iter .from (a) |> Iter .map(f) |> Iter .filter(f) |> Iter .collect()
CLI #
bash Copy
zolo run file.zolo # run
zolo compile file.zolo # show Lua output
zolo check file.zolo # type check only
zolo test file.zolo # run @test functions
zolo fmt file.zolo # format
zolo repl # interactive REPL
Lua Comparison #
Zolo
Lua
let x = 10
local x = 10
fn f() { }
function f() end
|x| x * 2
function(x) return x*2 end
arr[0 ]
arr[1 ]
if c { }
if c then end
a |> f()
f(a)
struct S { }
table + metatable
enum E { A(x) }
{__tag="A", x}