Skip to main content

Overloads

Several functions can share a name, as long as their parameter types differ. A call runs the one whose parameters match what it passes:

let describe(value: i32) = "a number"
let describe(value: string) = "some text"

let first = describe(42)
let second = describe("hello")

In a type​

A class, object or trait can overload its members the same way, and most overloads you'll meet are there. A type that can be written to in more than one way gets one member per way:

class Display {
var shown = ""

let show(text: string) {
shown = text
}

let show(number: i32) {
shown = "${number}"
}
}

entrypoint main() {
let display = Display()
display.show("ready")
display.show(3)
}

Choosing a candidate​

A call runs the candidate whose parameter types match its arguments. If none matches exactly, a number can widen to a bigger type of its kind, as it can anywhere: an i32 can call an i64 version, and an f32 an f64 one. A whole number never becomes a float by itself, so convert it to call a float version:

let scale(value: f64) = value * 2.0
let scale(value: string) = "${value} twice"

let count = 7
let answer = scale(f64(count))