SultanC
  1. Home
  2. Examples
  3. Compile time

Decorators wrap a function with another ordinary function

comptime/decorators.sn
// Decorators wrap a function with another ordinary function.
import std.io.println;
import std.text.number;

def doubled(comptime f: fn(int): int, value: int): int {
    return f(value) * 2;
}

def plus_one(comptime f: fn(int): int, value: int): int {
    return f(value) + 1;
}

@plus_one
@doubled
def square(value: int): int { return value * value; }

def main(): int {
    println(int_to_text(square(5)));
    return 0;
}

Run it

With the interpreter, without compiling:

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

Or compile it to an executable:

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

Source on GitHub   All examples