This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/_archive/rhai_engine/rhaibook/language/overload.md
2025-04-04 08:28:07 +02:00

835 B
Raw Blame History

Function Overloading

{{#include ../links.md}}

[Functions] defined in script can be overloaded by arity (i.e. they are resolved purely upon the function's name and number of parameters, but not parameter types since all parameters are the same type [Dynamic]).

New definitions overwrite previous definitions of the same name and number of parameters.

fn foo(x, y, z) {
    print(`Three!!! ${x}, ${y}, ${z}`);
}
fn foo(x) {
    print(`One! ${x}`);
}
fn foo(x, y) {
    print(`Two! ${x}, ${y}`);
}
fn foo() {
    print("None.");
}
fn foo(x) {     // <- overwrites previous definition
    print(`HA! NEW ONE! ${x}`);
}

foo(1,2,3);     // prints "Three!!! 1,2,3"

foo(42);        // prints "HA! NEW ONE! 42"

foo(1,2);       // prints "Two!! 1,2"

foo();          // prints "None."