3. Tipos Agregados

Arrays

Arrays de tamaño fijo con semántica de valor.

def SIZE = 5;
let ints: int[SIZE] = [1, 2, 3, 4, 5];
let zeros: [int; SIZE]; // Inicializado a cero
#define SIZE 5
int ints[SIZE] = {1, 2, 3, 4, 5};
int zeros[SIZE] = {0};

Tuplas

Agrupa múltiples valores, accede a los elementos por índice.

let pair = (1, "Hola");
let x = pair.0;  // 1
let s = pair.1;  // "Hola"
typedef struct { int v0; const char *v1; } Tuple_int_string;

Tuple_int_string pair = {1, "Hola"};
int x = pair.v0;          // 1
const char *s = pair.v1;  // "Hola"

Múltiples Valores de Retorno

Las funciones pueden retornar tuplas para proporcionar múltiples resultados:

fn sumar_y_restar(a: int, b: int) -> (int, int) {
    return (a + b, a - b);
}

let resultado = sumar_y_restar(3, 2);
let suma = resultado.0;   // 5
let resta = resultado.1;  // 1
typedef struct { int v0; int v1; } Tuple_int_int;

Tuple_int_int sumar_y_restar(int a, int b) {
    return (Tuple_int_int){a + b, a - b};
}

Tuple_int_int resultado = sumar_y_restar(3, 2);
int suma  = resultado.v0;  // 5
int resta = resultado.v1;  // 1

Desestructuración

Las tuplas pueden desestructurarse directamente en variables:

let (suma, resta) = sumar_y_restar(3, 2);
// suma = 5, resta = 1
Tuple_int_int tmp = sumar_y_restar(3, 2);
int suma = tmp.v0;
int resta = tmp.v1;
// suma = 5, resta = 1

La desestructuración tipada permite anotaciones de tipo explícitas:

let (a: string, b: u8) = ("hello", 42);
let (x, y: i32) = (1, 2);  // Mixto: x inferido, y explícito
const char *a = "hello";
uint8_t b = 42;
int x = 1;
int32_t y = 2;

Structs

Estructuras de datos con campos de bits opcionales.

struct Point {
    x: int;
    y: int;
}

// Inicialización de struct
let p = Point { x: 10, y: 20 };

// Campos de bits
struct Flags {
    valid: U8 : 1;
    mode:  U8 : 3;
}
typedef struct {
    int x;
    int y;
} Point;

// Inicialización de struct
Point p = { .x = 10, .y = 20 };

// Campos de bits
typedef struct {
    uint8_t valid : 1;
    uint8_t mode : 3;
} Flags;
Note

Los structs usan Semántica de Movimiento por defecto. Los campos se pueden acceder mediante . incluso en punteros (Auto-Dereferencia).

Structs Opacos

Puedes definir un struct como opaque para restringir el acceso a sus campos solo al módulo que lo define, permitiendo aún que el struct sea asignado en el stack (el tamaño es conocido).

// En user.zc
opaque struct User {
    id: int;
    name: string;
}

fn new_user(name: string) -> User {
    return User{id: 1, name: name}; // OK: Dentro del módulo
}

// En main.zc
import "user.zc";

fn main() {
    let u = new_user("Alice");
    // let id = u.id; // Error: No se puede acceder al campo privado 'id'
}
// En user.h
typedef struct {
    int id;
    const char *name;
} User;

User new_user(const char *name);

// En user.c
User new_user(const char *name) {
    return (User){ .id = 1, .name = name };
}

// En main.c
#include "user.h"

int main(void) {
    User u = new_user("Alice");
    // -- ¡Error de compilación de Zen C! --
    // int id = u.id; // Zen C se negaría a transpilar esto y daría un error.
}

Enums

Uniones etiquetadas (Tipos suma) capaces de contener datos.

enum Shape {
    Circle(float),      // Contiene el radio
    Rect(float, float), // Contiene ancho y alto
    Point               // Sin datos
}
typedef enum { Shape_Circle_Tag, Shape_Rect_Tag, Shape_Point_Tag } Shape_Tag;

typedef struct { float v0; float v1; } Tuple_float_float;

typedef struct {
    Shape_Tag tag;
    union {
        float Circle;
        Tuple_float_float Rect;
    } data;
} Shape;

Shape Shape_Circle(float v) {
    return (Shape){ .tag = Shape_Circle_Tag, .data.Circle = v };
}

Shape Shape_Rect(float v0, float v1) {
    return (Shape){ .tag = Shape_Rect_Tag, .data.Rect = { v0, v1 } };
}

Shape Shape_Point(void) {
    return (Shape){ .tag = Shape_Point_Tag };
}

Uniones

Uniones estándar de C (acceso inseguro).

union Data {
    i: int;
    f: float;
}
typedef union {
    int i;
    float f;
} Data;

Vectores SIMD

Tipos de vectores SIMD nativos utilizando extensiones de vectores de GCC/Clang. Anota un struct con @vector(N) para definir un vector de N elementos.

import "std/simd.zc";

fn main() {
    let a = f32x4{v: 1.0};              // Difusión: {1.0, 1.0, 1.0, 1.0}
    let b = f32x4{1.0, 2.0, 3.0, 4.0};  // Inicialización por elemento
    let c = a + b;                       // Suma por elementos
    let x = c[0];                        // Acceso a elementos (float)
}
typedef float f32x4 __attribute__((vector_size(16)));

int main(void) {
    f32x4 a = {1.0f, 1.0f, 1.0f, 1.0f};
    f32x4 b = {1.0f, 2.0f, 3.0f, 4.0f};
    f32x4 c = a + b;
    float x = c[0];
}

Los operadores aritméticos (+, -, *, /) y bit a bit (&, |, ^) funcionan por elementos. Consulta std/simd.zc para los tipos predefinidos.

Alias de Tipos

Crea un nuevo nombre para un tipo existente.

alias ID = int;
alias PointMap = Map<string, Point>
alias OpFunc = fn(int, int) -> int
typedef int ID;
typedef struct Map_string_Point PointMap;
typedef struct { void *func; void *ctx; void (*drop)(void *); } z_closure_T;
typedef z_closure_T OpFunc;

Nota: El punto y coma final es opcional para los alias de tipo.

Alias de Tipos Opacos

Puedes definir un alias de tipo como opaque para crear un nuevo tipo que sea distinto de su tipo subyacente fuera del módulo que lo define. Esto proporciona una fuerte encapsulación y seguridad de tipos sin la sobrecarga en tiempo de ejecución de un struct envoltorio.

// En library.zc
opaque alias Handle = int;

fn make_handle(v: int) -> Handle {
    return v; // Conversión implícita permitida dentro del módulo
}

// En main.zc
import "library.zc";

fn main() {
    let h: Handle = make_handle(42);
    // let i: int = h; // Error: Falló la validación de tipos
    // let h2: Handle = 10; // Error: Falló la validación de tipos
}
// En library.h
typedef int Handle;

Handle make_handle(int v);

// En library.c
Handle make_handle(int v) {
    return v;
}

// En main.c
#include "library.h"

int main(void) {
    Handle h = make_handle(42);
    // -- ¡Error de compilación de Zen C! --
    // int i = h;       // Zen C se negaría a transpilar esto y daría un error.
    // Handle h2 = 10;  // Zen C se negaría a transpilar esto y daría un error.
}