นำหน้า
Computes a module constant's value at build time.
Translation status: ภาษาไทย reader-locale proof. Term names and code fences follow the th-TH pack; supporting prose may still be English.
Computes a module constant's value at build time.
Syntax: const <type> <name> = comptime { … return <value> }
Category#
comptime
Related#
Examples#
radix/corpus/praefixum/praefixum.fab (canonical · keyword)#
Computes a module constant's value at build time.
# =============================================================================
# นำหน้า — Computes a module constant's value at build time.
# =============================================================================
#
# What this teaches:
# • A `comptime { … }` body is the initializer of a module constant. It runs once,
# during the build, and `return`s the value; the program only ever sees
# the finished literal.
# • The body is ordinary Faber: loops, `if`, locals, collections, ชนิด
# values, and calls to pure functions.
# • Module constants may read other module constants; the build evaluates
# them in dependency order.
#
# Common mistakes:
# • Using `comptime` outside a constant initializer — it is legal only as
# the whole initializer of `const T NAME = comptime { … }` (module level or
# block level) or of a class field default.
# • The retired `comptime(expr)` form — a plain expression needs no marker:
# `const int N = 4 * 1024` already folds at compile time.
# • Reaching I/O, `call`, or async code — the build rejects the constant and
# names the call chain that got there.
#
# See also: ของชนิด, ฝัง
# =============================================================================
# GRAMMAR:
# fixum_decl :← 'คงที่' type_annotation IDENTIFIER '=' const_init
# const_init :← expression
# praefixum_expr :← 'นำหน้า' block_stmt
#
# EXPECTED OUTPUT:
# praefixum.expected — the values computed at build time.
class Punctum {
const int x
const int y
}
fn quadratum(int n) → int {
return n * n
}
# A loop that calls a pure function.
const int SUMMA_QUADRATORUM = comptime {
var int total ← 0
for range 1‥11 const i {
total ← total + quadratum(i)
}
return total
}
# A collection built at build time.
const list<int> PRIMI = comptime {
var list<int> found ← empty
for range 2‥30 const n {
var bool isPrime ← true
for range 2 ‥ n const d {
if n % d ≡ 0 {
isPrime ← false
}
}
if isPrime {
found.append(n)
}
}
return found
}
# Text and a ชนิด value.
const string LINEA = comptime {
var string stripe ← ""
for range 0‥8 const i {
stripe ← stripe + "="
}
return stripe
}
const Punctum ORIGO = comptime {
return Punctum { x = SUMMA_QUADRATORUM % 10, y = -3 }
}
# A plain module constant reading a build-time one folds after it.
const int DUPLEX = SUMMA_QUADRATORUM * 2
main {
print SUMMA_QUADRATORUM
print PRIMI
print LINEA
print ORIGO.x
print ORIGO.y
print DUPLEX
}Expected output:
385
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
========
5
-3
770