रेंडरिंगhi

उपसर्ग

Computes a module constant's value at build time.

Translation status: हिन्दी reader-locale proof. Term names and code fences follow the hi 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

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