Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Match Priorities

A single parameter can match an argument in several ways, each with a defined priority. These priorities become important when calling workbenches which support overloaded initialization.

Priority
⭣ high to low
MatchesExample ParametersExample Arguments
Empty ListEmpty arguments with empty parameters()()
IdentifierMatch argument identifier with parameter identifier(x: Scalar)(x=1)
Shortened IdentifierMatch argument identifier with shortened parameter identifier(max_height: Scalar)(m_h=1)
TypeMatch argument type with parameter type(x: Length)(1mm)
Compatible TypeMatch argument type with compatible parameter type(x: Scalar)f(1)
DefaultMatch parameter defaults(x=1mm)()

The match strategy is to try all priorities in order from highest (Empty) to lowest (Default) until all arguments match a parameter.

Match Empty List

Matches when both the arguments and parameters are empty.

test

fn f() {} // no parameters

f(); // no arguments

Match Identifier

The following example demonstrates calling a function f with each argument specified by name:

test

fn f(width: Length, height: Length) -> Area { width * height }

x = f(height = 2cm, width = 1cm); // call f() with parameters in arbitrary order

std::debug::assert_eq([x, 2cm²]);

Match Short Identifier

A parameter can also be matched using it's short identifier.

The short form consists of the first letter of each word separated by underscores (_).

test

fn f(width: Length, height: Length) -> Area { width * height }

// use short identifiers
std::debug::assert_eq([f(w = 1cm, h = 2cm), 2cm²]);
// can be mixed
std::debug::assert_eq([f(w = 1cm, height = 2cm), 2cm²]);

Here are some usual examples of short identifiers:

IdentifierShort Identifier
parameterp
my_parameterm_p
my_very_long_parameter_namem_v_l_p_n
my_Parameterm_P
MyParameterM
myParameterm

Match Type

Nameless values can be used if all parameter types of the called function (or workbench) are distinct.

test

fn f(a: Scalar, b: Length, c: Area) {} // warning: unused a,b,c
// Who needs names?
f(1.0, 2cm, 3cm²);

Match Compatible Type

Nameless arguments can also be compatible with parameter types, even if they are not identical.

test

fn f(a: Scalar, b: Length, c: Area) {} // warning: unused a,b,c
// giving an integer `1` to a `Scalar` parameter `a`
f(1, 2cm, 3cm²);

Match Default

If an argument is not provided and its parameter has a default value defined, the default will be used.

test

fn f(a = 1mm) {} // warning: unused a
// a has default
f();

Mix'em all

You can combine all these methods.

test

fn f(a: Scalar, b: Length, c = 2cm, d: Length) -> Volume {} // warning: unused a,b,c,d

// `a` gets the Integer (1) which is compatible to Scalar (1.0)
// `b` is named
// `c` gets it's default
// `d` does not need a name because `b` has one
f(b = 2cm, 1, 3cm);