⛅ Coming soon: create Unison Cloud clusters in minutes on your own infrastructure. Learn more

Defining operators

Operators are functions that take two arguments and are called with the name of the function between its arguments, as in 4 Nat.* 3. We call this kind of syntax, "infix notation". You can write your own operators in Unison with a few special syntax conventions.

Operator names in Unison can only be comprised of the characters !$%^&*-=+<>~\\/|:. So ==> is a valid operator name but =hi!> is not.

You can wrap your operator in parenthesis followed by two arguments to define it.

(^) x y = Nat.pow x y

Or you can define the operator with the symbols between its arguments:

x ^ y = Nat.pow x y

Without the parenthesis or infix definition, the UCM would fail to parse ^ as a valid function definition.

Calling these functions can be done with the infix notation, 2 ^ 3, or with the regular function application syntax, ^ 2 3.

You might want your operator to be prefixed with a namespace so it's located in a specific place in your codebase. Prepend the namespace inside the parentheses:

(aNamespace.^) x y = Nat.pow x y

Or add it in between the arguments:

x aNamespace.^ y = Nat.pow x y