Nesta página
Guia Rápido do Zolo
Variáveis¶
let x = 10 // imutável
let mut y = 0 // mutável
const PI = 3.14 // constanteTipos¶
int f64 str bool nil
[int] // array
{str: int} // map
(int, str) // tupla
int? // opcional
fn(int) -> int // tipo de função
Map<str, int> // genéricoFunções¶
fn add(a: int, b: int) -> int { a + b }
let double = |x| x * 2 // lambda
fn apply(f: fn(int) -> int, x: int) -> int { f(x) }Fluxo de Controle¶
if cond { } else if cond2 { } else { }
let x = if cond { a } else { b } // expressão if
if let Some(v) = opt { use(v) } // if let
match val { 0 => "zero", _ => "other" }Loops¶
for i in 0..10 { } // intervalo exclusivo
for i in 0..=10 { } // intervalo inclusivo
for item in list { } // iterar
for (i, v) in pairs { } // desestruturar
while cond { }
while let Some(x) = iter() { } // while let
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 { "..." } }Correspondência de Padrão¶
match value {
0 => "zero",
n if n < 0 => "negative",
Shape::Circle(r) => "circle {r}",
Point { x, y } => "({x},{y})",
'a' | 'b' => "ab",
_ => "other",
}Operadores¶
// Aritméticos: + - * / % **
// Comparação: == != < > <= >=
// Lógicos: && || !
// Atribuição: = += -= *= /= %=
// Pipe: |> a |> f() → f(a)
// Tap: &. a &. f() → f(a); a
// Opcional: ?. a?.b → nil se a for nil
// Coalescência: ?? a ?? b → b se a for nil
// Propagação: ? expr? → retorno antecipado em erro
// Spread: ... [...a, ...b]
// Intervalo: .. 0..10 (exclusivo)
// Intervalo inc:..= 0..=10 (inclusivo)Strings¶
"Hello, {name}!" // interpolação
"""multiline""" // multilinha
sql"SELECT * FROM t WHERE id={x}" // template com tagDecoradores¶
@test fn test_it() { ... } // função de teste
@memoize fn fib(n) { ... } // resultados em cache
@deprecated("msg") fn old() { } // aviso de obsolescência
@builder struct Config { ... } // padrão builderBiblioteca Padrão¶
// String
string.trim(s) string.split(s, sep) string.contains(s, sub)
// Array
Array.map(a, f) Array.filter(a, f) Array.reduce(a, f, init)
Array.push(a, v) Array.len(a) Array.sort(a)
// Map
Map.new() Map.set(m, k, v) Map.get(m, k) Map.keys(m)
// Set
Set.from(arr) Set.add(s, v) Set.has(s, v) Set.union(a, b)
// Option
Option.Some(v) Option.None() Option.unwrap_or(o, def)
// Result
Result.Ok(v) Result.Err(e) Result.unwrap(r)
// Iterator
Iter.from(a) |> Iter.map(f) |> Iter.filter(f) |> Iter.collect()CLI¶
zolo run file.zolo # executar
zolo compile file.zolo # exibir saída Lua
zolo check file.zolo # verificar tipos apenas
zolo test file.zolo # executar funções @test
zolo fmt file.zolo # formatar
zolo repl # REPL interativoComparação com Lua¶
| 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 { } |
tabela + metatabela |
enum E { A(x) } |
{__tag="A", x} |