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 returns a value, you can omit the curly braces and the return keyword and instead use the lambda arrow (=>) syntax:

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:

Lambda expressions

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

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

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

Advanced: Variadic functions

A variadic function is a convenience allowing you to pass a sequence argument as individual arguments. A variadic argument is marked by three periods (...) before the argument name.

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.

Usually, the type variables of a function can be inferred from usage (as in the example above). Sometimes, however, it cannot — and then you have to specify the type variables yourself. This is known as specialization.

Advanced: Function composition using the >> operator

Composition is a terse way of creating a new function from a pair of functions. The resulting function takes the same arguments as the left-hand side and, calls it with those arguments, and passes the result as a single argument to right-hand side function, returning its result.

In other words, for functions f and g, the composition f >> g is equivalent to the lambda expression x => g(f(x)) .

Advanced: Partial function calls using the _ operator

It is possible to partially call a function. A partial call is a way to define a new function from an existing one, "fixing" some of the parameters to set values while leaving others for later. The resulting function only requires the "leftover" arguments.

Last updated

Was this helpful?