Variables y Tipos
Bindings (let/var), anotaciones y los tipos integrados principales.
Variables y Tipos
Sounio es un lenguaje con tipado estático, con inferencia de tipos para la mayoría de los bindings locales.
Declaración de Variables
Variables Inmutables (let)
Por defecto, las variables son inmutables:
let x = 42 // Type inferred as i32
let name = "Alice" // Type inferred as string
let pi = 3.14159 // Type inferred as f64
Variables Mutables (var)
Usa var para variables mutables:
var counter = 0
counter = counter + 1 // OK
let fixed = 10
fixed = 20 // Error: cannot assign to immutable variable
Anotaciones de Tipo
Puedes anotar tipos explícitamente:
let x: i32 = 42
let ratio: f64 = 0.5
let flag: bool = true
let message: string = "Hello"
Tipos Primitivos
| Tipo | Descripción | Ejemplo |
|---|---|---|
i8, i16, i32, i64 | Enteros con signo | let x: i32 = -42 |
u8, u16, u32, u64 | Enteros sin signo | let y: u32 = 42 |
f32, f64 | Punto flotante | let pi: f64 = 3.14 |
bool | Booleano | let flag: bool = true |
char | Carácter Unicode | let c: char = 'A' |
string / String | Cadena de texto | let s = "hello" |
str | Slice de string prestado | let s: &str = "hello" |
Tipos Compuestos
Tuplas
let point = (10, 20)
let first = point.0 // 10
let second = point.1 // 20
Arrays
let numbers: [i32; 5] = [1, 2, 3, 4, 5]
let first = numbers[0] // 1
Vectores (Vec<T>)
let items: Vec<i32> = [1, 2, 3, 4]
for x in items {
// ...
}
Inferencia de Tipos
Sounio infiere tipos a partir del contexto:
let values = [1, 2, 3] // Array literal
let vec_values: Vec<i32> = values
Nota de la spec: las unidades de medida se parsean (por ejemplo,
unit kg; unit g = 0.001 * kg; unit mg = 0.001 * g;), pero la verificación completa de unidades puede estar habilitada solo en algunos modos/flags del compilador.
Tipos Epistémicos (Knowledge<T>)
Para valores con incertidumbre:
let k = Knowledge { value: 42.0 }
let x: f64 = k.unwrap("accepted for demo")
Consulta Knowledge Types para más detalles.