Skip to content

Functions

Benjamin Dupont edited this page Aug 26, 2020 · 3 revisions

Free functions

Free functions can be declared as top-level items.

function square(n: i32): i32 {
    return n * n
}

For functions returning a single expression, the => syntax can be used.

function square(n: i32): i32 => n * n

If the return type can be inferred, it can be omitted.

function square(n: i32) => n * n

Extension functions

Extension functions are functions that can only be called in a particular context.

function (i32).square() => this * n

25.square()

Attributes

Functions can have attributes.

[<attribute_list>]
function doThing() {
    // ...
}

Tag attributes

Tag attributes simply indicate their presence on a function.

[unsafe, experimental]
function doDanegrousThing() {
    // ...
}

Value attributes

Value attributes associate a particular value to some characteristic they define.

[path = "/home"]
function homepage(): Http.Response {
    // ...
}

Complex value attributes

Complex value attributes allow defining complex metadata on functions.

[Command(name: "echo", rateLimit: 15, permission: "commands.echo")]
function echoCommand() {
    // ...
}

Tests

Test functions can be defined as so with the test keyword.

Assertions use the assert keyword on a boolean expression.

assert can be used before a block to assert an operation does not throw an error.

test function shouldWork() {
    assert 1 == 1

    assert {
        operation()
    }
}

Test functions cannot be called directly.

Clone this wiki locally