Functions
Declaring functions, return values, tuples, closures, function types and recursion.
Functions are declared with fn. The body is a block, and the last expression
is not an implicit return, you must write return yourself.
fn add(a: int, b: int) -> int {
return a + b
}
Every parameter and every return type must be annotated. A parameter without a type is an error.
The entry point
run and test look for main. It takes no parameters and returns void:
fn main() {
println("hello")
}
A module with no main is still valid, you can check it or test it, but
run fails with module has no main; nothing to run.
Control flow inside a function
fn sign(n: int) -> string {
if (n > 0) {
return "positive"
} else if (n < 0) {
return "negative"
}
return "zero"
}
return exits the function immediately. Because if is a statement and not an
expression, the shape above, early returns, is the idiomatic pattern. There is
no ternary operator and no if expression:
fn main() {
// let x = if true { 1 } else { 2 } // syntax error
let x = 1
println(x)
}
Multiple return values
Return a tuple, and destructure it with a parenthesised pattern:
fn divmod(a: int, b: int) -> (int, int) {
return (a / b, a % b)
}
fn main() {
let (q, r) = divmod(17, 5)
println("q={q} r={r}") // q=3 r=2
}
Destructure a tuple-valued binding in a second step as well:
fn divmod(a: int, b: int) -> (int, int) {
return (a / b, a % b)
}
fn main() {
let (q, r) = divmod(9, 2)
println("q={q} r={r}") // q=4 r=1
}
Destructuring is how you read a tuple.
Closures
An anonymous function is a fn expression. It captures the bindings it
references:
fn main() {
let double = fn(x: int) -> int { return x * 2 }
println(double(21)) // 42
let offset = 10
let addOffset = fn(x: int) -> int { return x + offset }
println(addOffset(5)) // 15
}
Function types
A function type is written with -> between the parameter list and the return
type, and a function with the right shape is assignable to it:
fn apply(f: (int) -> int, v: int) -> int {
return f(v)
}
fn twice(n: int) -> int {
return n * 2
}
fn main() {
println(apply(twice, 21)) // 42
println(apply(fn(x: int) -> int { return x + 1 }, 1))
}
A void function has the type (...) -> void, and a function that takes no
parameters is () -> T.
Recursion
Recursion works, and is idiomatic for trees and algorithms:
fn fib(n: int) -> int {
if (n < 2) {
return n
}
return fib(n - 1) + fib(n - 2)
}
fn main() {
println(fib(15))
}
Declare the function before the recursive call site, the resolver requires it.
Passing arguments
A function takes exactly the parameters it declares. Call it with every argument, in order:
fn greet(name: string, greeting: string) -> string {
return "{greeting}, {name}!"
}
fn main() {
println(greet("ada", "Hello"))
println(greet("ada", "Hi"))
}
When one call site varies, an overload or a wrapper keeps it readable:
fn greetWith(name: string) -> string {
return greet(name, "Hello")
}
Methods
Functions can be declared inside a class, struct, enum or interface body.
They are written without a self parameter and are called with .:
class Greeter {
var name: string
constructor(name: string) {
this.name = name
}
fn greet() -> string {
return "hello, {this.name}"
}
}
fn main() {
let g = Greeter("ada")
println(g.greet())
}
