Skip to content

Decorators

Rohan Singh edited this page Jun 4, 2017 · 2 revisions

Decorators are special functions that wrap other functions to add or change behavior.

fun deprecated(function, message) {
    var firstCall = true;

    return fun(...args) {
        if (firstCall) {
            printLn("deprecated: " + message);
            firstCall = false;
        }

        return function(...args);
    }
}

@deprecated("example() will be removed in a future version")
fun example() {
    printLn("hello");
}

Usage

Decorators can be applied to any named function or sequence. Multiple decorators can be used on the same function and are called in order.

@x
@x.y
@x.y(z)
fun add(a, b) -> a + b;

The first argument passed to a decorator is always the function being decorated. When multiple decorators are used the second decorator will be passed the function that the first returned, the third will be passed what the second returned, and so on.