SultanC
  1. Home
  2. Examples
  3. Compile time

`compile_error` rejects a bad compile-time argument while compiling

comptime/compile_error.sn
// `compile_error` rejects a bad compile-time argument while compiling.
// Changing `checked_width(8)` to `checked_width(80)` stops the build.
import std.io.println;
import std.text.number;
import std.meta.reflect;

def checked_width(comptime bits: int): int {
    if (bits > 64) {
        compile_error("width must be at most 64 bits");
    };
    return bits;
}

def main(): int {
    println(int_to_text(checked_width(8)));
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/comptime/compile_error.sn

Or compile it to an executable:

./build/sultanc examples/en/comptime/compile_error.sn -o build/example && ./build/example

Source on GitHub   All examples