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

User-Defined Operators

Description

An infix operator is defined like an ordinary function, with the operator name enclosed in parentheses at the name position and the operands listed as a single parameter list:

def (+^)((a: i32, b: i32), (c: i32, d: i32)) = (a + c, b + d)

Call sites use the operator in infix position: (1, 2) +^ (3, 4). The parenthesised-name form is the only way to declare an operator.

Operator Names and Fixity

A valid operator name is a non-empty sequence of characters chosen from the string "+-*/%=!><&^|". The fixity of an operator is determined by its leading characters, which must correspond to a built-in operator. Thus +^ binds like +, while *^ binds like *. The longest such prefix wins, so >>= binds like >>, not like >.

Restrictions

It is not permitted to define operators with the names && or || (although these as prefixes are accepted). A user-defined version of either would not be short-circuiting. User-defined operators behave exactly like ordinary functions, except for being infix.

Shadowing Built-in Operators

A built-in operator may be shadowed (e.g. a new + can be defined). The built-in polymorphic operator then becomes inaccessible except through the intrinsics module.