Skip to content

Commit b252e5d

Browse files
committed
Add examples for custom (post)circumfix operators
1 parent 3d85453 commit b252e5d

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/Language/functions.pod

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,36 @@ sub postfix:<!>(Int $x where { $x >= 0 }) { [*] 1..$x };
277277
say 6!; # 720
278278
=end code
279279
280-
=begin comment
281-
TODO: elaborate on (post)circumfix ops
282-
=end comment
280+
The operator declaration becomes available as soon as possible, so you can
281+
even recurse into a just-defined operator, if you really want to:
282+
283+
=begin code
284+
sub postfix:<!>(Int $x where { $x >= 0 }) {
285+
$x == 0 ?? 1 !! $x * ($x - 1)!
286+
}
287+
say 6!; # 720
288+
=end code
289+
290+
Circumfix and postcircumfix operators are made of two delimiters, one opening
291+
and one closing.
292+
293+
=begin code
294+
sub circumfix:<START END>(*@elems) {
295+
"start", @elems, "end"
296+
}
297+
298+
say START 'a', 'b', 'c' END; # start a b c end
299+
=end code;
300+
301+
Postcircumfixes also receive the the term after which they are parsed as
302+
an argument:
303+
304+
=begin code
305+
sub postcircumfix:<!! !!>($left, $inside) {
306+
"$left -> ( $inside )"
307+
}
308+
say 42!! 1 !!; # 42 -> ( 1 )
309+
=end code
283310
284311
=head2 Precedence
285312

0 commit comments

Comments
 (0)