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.