Skip to content

Commit

Permalink
Moves op declaration to new page closes #2162
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Nov 4, 2018
1 parent a37af0a commit 8d9b988
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
1 change: 1 addition & 0 deletions doc/Language/00-POD6-CONTROL
Expand Up @@ -36,6 +36,7 @@ Iterating FILE: iterating
Module development utilities FILE: modules-extra
Module packages FILE: module-packages
Modules FILE: modules
Operators FILE: optut
Regexes best practices and gotchas FILE: regexes-best-practices

section: General reference
Expand Down
35 changes: 35 additions & 0 deletions doc/Language/optut.pod6
@@ -0,0 +1,35 @@
=begin pod
=TITLE Creating operators
=SUBTITLE A short tutorial on how to declare operators and create new ones.
Operators are declared by C<sub>
followed by C<prefix>, C<infix>, C<postfix>, C<circumfix>, or C<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␤»
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.␤»
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
29 changes: 3 additions & 26 deletions doc/Type/Sub.pod6
Expand Up @@ -7,32 +7,9 @@
class Sub is Routine { }
A type for subroutines and operators. Subs are created with the C<sub>
declarator keyword followed by an optional L<identifier|/language/syntax#Identifiers>. Operators are declared by C<sub>
followed by C<prefix>, C<infix>, C<postfix>, C<circumfix>, or C<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␤»
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.␤»
declarator keyword followed by an optional
L<identifier|/language/syntax#Identifiers>. This
L<short tutorial explains how operators are declared|/language/optut>.
Note that subs that go by the same name as
L<coercers|/language/typesystem#Coercion> will not take precedence over
Expand Down

0 comments on commit 8d9b988

Please sign in to comment.