Skip to content

Commit 6b25b8a

Browse files
committed
Merge branch 'master' of github.com:perl6/doc
2 parents 26f60cd + 3d85453 commit 6b25b8a

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/Language/functions.pod

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,30 @@ attach to the outer routine.
256256
257257
=head1 Defining Operators
258258
259-
TODO
259+
Operators are just subroutines with funny names. The funny names are composed
260+
of the category name (C<infix>, C<prefix>, C<postfix>, C<circumfix>,
261+
C<postcircumfix>), followed by a colon, and a list of the operator name or
262+
names (two components in the case of circumfix and postcircumfix).
263+
264+
This works both for adding multi candidates to existing operators, and for
265+
defining new ones. In the latter case, the definition of the new subroutine
266+
automatically installs the new operator into the grammar, but only in the
267+
current lexical scope. Importing an operator via C<use> or C<import> also
268+
makes it available.
269+
270+
=begin code
271+
# adding a multi candidate to an existing operator:
272+
multi infix:<+>(Int $x, "same") { 2 * $x };
273+
say 21 + "same", # 42
274+
275+
# defining a new operator
276+
sub postfix:<!>(Int $x where { $x >= 0 }) { [*] 1..$x };
277+
say 6!; # 720
278+
=end code
279+
280+
=begin comment
281+
TODO: elaborate on (post)circumfix ops
282+
=end comment
260283
261284
=head2 Precedence
262285

lib/Language/operators.pod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Operators can occur in several positions relative to a term:
9090
9191
=end table
9292
93-
Each operator is also available as a routine; postcircumfix operators as
94-
methods, all others as subroutines. The name of the routine is formed of
93+
Each operator is also available as a subroutine.
94+
The name of the routine is formed of
9595
the operator category, then a colon, and a list quote construct with the
9696
symbol(s) that make up the operator:
9797
@@ -103,8 +103,9 @@ term or as a prefix. Subroutine calls are the most common listops. Other
103103
cases include meta-reduced infix operators (C<[+]| 1, 2, 3>) and the
104104
L<#prefix ...> etc. stub operators.
105105
106-
(Niecza currently turns postcircumfix operators in a subroutine call,
107-
while Rakudo interprets them as methods).
106+
107+
Defining custom operators is covered in
108+
L</language/functions#Defining Operators>.
108109
109110
=head1 Z<>Term Precedence
110111

0 commit comments

Comments
 (0)