Creating operators

A short tutorial on how to declare operators and create new ones.

Operators are declared by using the sub keyword followed by prefix, infix, postfix, circumfix, or postcircumfix; then a colon and the operator name in a quote construct. For (post-)circumfix operators separate the two parts by white space.

sub hello {
        say "Hello, world!";
    }
say &hello.^name;   # OUTPUT: «Sub␤»
    hello;              # OUTPUT: «Hello, world!␤»
my $s = sub ($a, $b) { $a + $b };
    say $s.^name;       # OUTPUT: «Sub␤»
    say $s(2, 5);       # OUTPUT: «7␤»
# Alternatively we could create a more
    # general operator to sum n numbers
    sub prefix:<Σ>( *@number-list ) {
        [+] @number-list
    }
say Σ (13, 16, 1); # OUTPUT: «30␤»
sub infix:<:=:>( $a is rw, $b is rw ) {
        ($a, $b) = ($b, $a)
    }
my ($num, $letter) = ('A', 3);
    say $num;          # OUTPUT: «A␤»
    say $letter;       # OUTPUT: «3␤»
# Swap two variables' values
    $num :=: $letter;
say $num;          # OUTPUT: «3␤»
    say $letter;       # OUTPUT: «A␤»
sub postfix:<!>( Int $num where * >= 0 ) { [*] 1..$num }
    say 0!;            # OUTPUT: «1␤»
    say 5!;            # OUTPUT: «120␤»
sub postfix:<♥>( $a ) { say „I love $a!“ }
    42♥;               # OUTPUT: «I love 42!␤»
sub postcircumfix:<⸨ ⸩>( Positional $a, Whatever ) {
        say $a[0], '…', $a[*-1]
    }
[1,2,3,4]⸨*⸩;      # OUTPUT: «1…4␤»
constant term:<♥> = "♥"; # We don't want to quote "love", do we?
    sub circumfix:<α ω>( $a ) {
        say „$a is the beginning and the end.“
    };
α♥ω;               # OUTPUT: «♥ is the beginning and the end.␤»

These operators use the extended identifier syntax; that is what enables the use of any kind of codepoint to refer to them.

See Also

Classes and objects

A tutorial about creating and using classes in Raku

CompUnits and where to find them

How and when Raku modules are compiled, where they are stored, and how to access them in compiled form.

Concurrency

Concurrency and asynchronous programming

Command line interface

Creating your own CLI in Raku

Grammar tutorial

An introduction to grammars

Input/Output

File-related operations

Inter-process communication

Programs running other programs and communicating with them

Iterating

Functionalities available for visiting all items in a complex data structure

Doing math with Raku

Different mathematical paradigms and how they are implemented in this language

Module packages

Creating module packages for code reuse

Core modules

Core modules that may be useful to module authors

Module development utilities

What can help you write/test/improve your module(s)

Modules

How to create, use, and distribute Raku modules

Regexes: best practices and gotchas

Some tips on regexes and grammars

REPL

Read-eval-print loop

Entering unicode characters

Input methods for unicode characters in terminals, the shell, and editors

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.