Saltar al contenido

Guía 15 de 36

Guía Rápida de Zolo

Referencia rápida de toda la sintaxis de Zolo.

En esta página

Variables

let x = 10              // inmutable
let mut y = 0           // mutable
const PI = 3.14         // constante

Tipos

int    f64    str    bool    nil
[int]                   // array
{str: int}              // map
(int, str)              // tupla
int?                    // opcional
fn(int) -> int          // tipo de función
Map<str, int>           // genérico

Funciones

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) }

Flujo de Control

if cond { } else if cond2 { } else { }
let x = if cond { a } else { b }           // expresión if
if let Some(v) = opt { use(v) }            // if let
match val { 0 => "zero", _ => "other" }

Bucles

for i in 0..10 { }             // rango exclusivo
for i in 0..=10 { }            // rango inclusivo
for item in list { }            // iterar
for (i, v) in pairs { }        // desestructurar
while cond { }
while let Some(x) = iter() { } // while let
loop { break }

Estructuras (struct)

struct Point { x: f64, y: f64 }
let p = Point { x: 1.0, y: 2.0 }
impl Point { fn dist(self) -> f64 { ... } }

Enumeraciones (enum)

enum Color { Red, Green, Blue }
enum Shape { Circle(f64), Rect(f64, f64) }
let s = Shape::Circle(5.0)

Rasgos (trait)

trait Display { fn show(self) -> str }
impl Display for Point { fn show(self) -> str { "..." } }

Coincidencia de Patrones

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:  + - * / % **
// Comparación:  == != < > <= >=
// Lógicos:      && || !
// Asignación:   = += -= *= /= %=
// Pipe:         |>      a |> f() → f(a)
// Tap:          &.      a &. f() → f(a); a
// Opcional:     ?.      a?.b → nil si a es nil
// Coalescencia: ??      a ?? b → b si a es nil
// Propagación:  ?       expr? → retorno anticipado en error
// Spread:       ...     [...a, ...b]
// Rango:        ..      0..10 (exclusivo)
// Rango incl:   ..=     0..=10 (inclusivo)

Cadenas

"Hello, {name}!"                 // interpolación
"""multiline"""                  // multilínea
sql"SELECT * FROM t WHERE id={x}" // plantilla con etiqueta

Decoradores

@test      fn test_it() { ... }  // función de prueba
@memoize   fn fib(n) { ... }     // resultados en caché
@deprecated("msg") fn old() { }  // advertencia de obsolescencia
@builder   struct Config { ... }  // patrón builder

Biblioteca Estándar

// Cadena
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           # ejecutar
zolo compile file.zolo       # mostrar salida Lua
zolo check file.zolo         # solo verificar tipos
zolo test file.zolo          # ejecutar funciones @test
zolo fmt file.zolo           # formatear
zolo repl                    # REPL interactivo

Comparación con 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 { } tabla + metatabla
enum E { A(x) } {__tag="A", x}

DOCS / OPINIÓN

¿Esta página dejó alguna pregunta?

Cuéntanos dónde te perdió la explicación. La documentación también forma parte de la experiencia del lenguaje.

Índice global

Encuentra tu camino por Zolo

Prueba una idea

Empieza aquí

9 resultados

9 resultados

enespt-br