Operators

Custom operators

Full

Define your own prefix, infix, and postfix operators as ordinary subs.

Operators in Raku are just subs with special names. Declaring sub infix:<…>, prefix:<…>, or postfix:<…> adds a new operator to the language — usable with the same syntax as the built-ins, Unicode symbols included.

A postfix operator #

The name postfix:<!> defines ! after its operand — here, factorial.

sub postfix:<!>($n) { [*] 1..$n }
say 5!;
Output
120

An infix operator #

sub infix:<×>($a, $b) { $a * $b }
say 6 × 7;
Output
42

A prefix operator #

sub prefix:<√>($x) { $x.sqrt }
say √16;
Output
4

Notes #