Function definitions

Functions

You can declare a new function using a function statement.

// Example: create a function which takes a number 'n' as argument
// and returns n squared.

function square(n: number) {
    return n * n;
}

return square(4);

If you omit the argument type for a function, it is assigned the type primitive.

The return type of a function is inferred from its body. It is also possible to specify it explicitly:

// Example: a function with an explicit return type.
function square(n: number) : number {
    return n * n;
}

return square(3);

All functions must return some value. Functions with explicitly declared return types can call themselves recursively.

For short function that immediately return a value, you can omit the curly braces and the return keyword and instead use the lambda arrow syntax:

// Example: a couple of functions declared using the lambda arrow syntax

function square(n: number) => n * n;

function abs(n: number) =>
    case when n < 0 then n * -1
         else n
         end;

Self functions

The first argument of a function may be decorated with the self keyword, turning the function into a self function. When calling a self function, the first argument may be supplied using dot notation:

function multiply(self n: number, m: number) : number {
    return n * m;
}

return 3.multiply(2); // the first argument is supplied by the left side of the dot.

Lambda expressions

A lambda expression is a terse syntax for creating anonymous function values. A lambda expression is written using an argument list followed by a lambda arrow and a single return expression.

// Example: declare a function using a lambda expression.
let add = (x, y) => x + y;
return add(2, 3);

For single-argument lambda expressions, the parantheses around the argument list may be omitted:

// Example: a single-argument lambda expression.
let square = x => x * 2;
return square(3);

Typically used in conjunction with the Seq module, lambda expressions provide the benefit of inferring their argument types from the context:

// Example: get a list of user names using the Seq.map function
// together with a lambda expression

open Seq;

let users = [
    {id: 0, name: "Gustava"},
    {id: 1, name: "Farid"}
];

return users.map(u => u.name); // here, 'u' gets its type from its surroundings.

Advanced: Generic functions

A generic function leaves some of its constituent types – argument types or return type – purposely undefined, replacing them with type variables. This allows the function to operate on different kinds of values while retaining type safety.

In the following example, the function second is defined with one type variable called T, declared in the angle brackets after the function name. This makes the function return number? when called on a list of numbers, text? when called on a list of texts, etc.

// Example: create a generic function which returns the second element
// of a sequence (or null if there are less than two elements).

function second<T>(self xs: T*): T? {
    return xs.skip(1).firstOrNull();
}

let secondNumber = [1, 2, 3].second(); // has type 'number?'
let secondText = ["hello", "world"].second(); // has type 'text?'

Last updated