stdlib e incrustación de archivos
En esta página
El subconjunto comptime de la stdlib cubre los casos más comunes: métodos de
string (trim, to_upper, to_lower, concatenación) y funciones de math.*
(sqrt, pow, floor, etc.). Úsalos de la misma forma que en tiempo de
ejecución — la diferencia es que todo ocurre dentro del compilador.
Banner normalizado con trim + to_upper y raíz cuadrada de 64 — ambos resueltos en la compilación.
// Feature: comptime stdlib bits — string methods and `math.*` work too
// Syntax: same `comptime { ... }` block; you can call a curated subset
// of stdlib helpers (string trim/upper, math.sqrt, etc.).
// When to use: bake banner strings, compile-time constants from
// numerical formulas, normalize identifiers at build time.
use std::math
let banner = comptime {
let raw = " Hello, Comptime! "
raw.trim().to_upper()
}
print(banner)
// expected: HELLO, COMPTIME!
let r = comptime math.sqrt(64.0)
print(r)
// expected: 8
Incrustar archivos con comptime fs.read
comptime fs.read("ruta") lee un archivo durante la compilación y reemplaza
la llamada por la string resultante. La ruta es siempre relativa al archivo
fuente, no al directorio de trabajo del compilador.
Usa esta funcionalidad para incrustar plantillas, prompts, esquemas JSON,
queries SQL o shaders directamente en el binario. No hay fopen en producción,
no hay riesgo de archivo ausente en el despliegue y no hay trabajo de
resolución de rutas en tiempo de ejecución.
El resultado es una string comptime normal — puedes encadenarlo con otros métodos comptime en la misma expresión.
Lectura de un archivo en tiempo de compilación y transformación de la string resultante.
// Feature: bake a file's contents into the program at compile time,
// reusing the runtime `std::fs` API under a `comptime` prefix.
// Syntax: `comptime fs.read("relative/path")` reads the file during
// compilation and replaces the call with the resulting string literal.
// Path resolution in comptime is **always relative to this source
// file**, not the compiler's CWD — see `specs/comptime-fs.md`.
// When to use: ship templates, prompts, JSON schemas, SQL queries, or
// shaders as string constants — no runtime fopen, no missing-file
// failures in production, no path resolution gymnastics.
use std::fs
let banner = comptime fs.read("07-embed-file.banner.txt")
print(banner)
// expected (once implemented):
// Welcome to Zolo!
// comptime makes constants free.
// You can pipe the result through other comptime methods, since the
// return is a normal comptime string.
let upper_banner = comptime {
fs.read("07-embed-file.banner.txt").trim().to_upper()
}
print(upper_banner)
// expected (once implemented):
// WELCOME TO ZOLO!
// COMPTIME MAKES CONSTANTS FREE.
Consulta también