Skip to content

Commit 0a3175f

Browse files
author
Brock Wilcox
committed
More syntax.pod additions, especially about operators
1 parent b7b41e1 commit 0a3175f

File tree

1 file changed

+58
-11
lines changed

1 file changed

+58
-11
lines changed

doc/Language/syntax.pod

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,12 @@ A L<Regex|/type/Regex> is declared with slashes like C</foo/>. Note that this C<
379379
380380
=head3 Variable declaration
381381
382-
my $x; # simple lexical variable
383-
my $x = 7; # initialize the variable
384-
my Int $x = 7; # declare the type
385-
my Int $x:D = 7; # specify that the value must be defined (not undef)
386-
my Int $x where * > 3 = 7; # constrain the value based on a function
382+
my $x; # simple lexical variable
383+
my $x = 7; # initialize the variable
384+
my Int $x = 7; # declare the type
385+
my Int $x:D = 7; # specify that the value must be defined (not undef)
386+
my Int $x where { $_ > 3 } = 7; # constrain the value based on a function
387+
my Int $x where * > 3 = 7; # same constraint, but using L<Whatever> short-hand
387388
388389
See L<Variable Declarators and Scope|http://docs.perl6.org/language/variables#Variable_Declarators_and_Scope> for more details on other scopes (our, has).
389390
@@ -400,9 +401,11 @@ You can also assign subroutines to variables.
400401
my &f = -> { say "Hello!" } # Lambda style syntax. The & sigil indicates the variable holds a function
401402
my $f = -> { say "Hello!" } # Functions can also be put into scalars
402403
403-
=head3 Package, Class, Role, and Grammar declaration
404+
=head3 Module, Class, Role, and Grammar declaration
404405
405-
package Gar { }
406+
There are several types of compilation units (packages), each declared with a keyword, a name, some optional traits, and a body of functions.
407+
408+
module Gar { }
406409
407410
class Foo { }
408411
@@ -412,11 +415,13 @@ You can also assign subroutines to variables.
412415
413416
You can declare a unit of things without explicit curly brackets.
414417
415-
unit package Gar;
418+
unit module Gar;
416419
# ... stuff goes here instead of in {}'s
417420
418421
=head3 Multi-dispatch declaration
419422
423+
See also L<Multi-dispatch|http://docs.perl6.org/language/functions#Multi-dispatch>.
424+
420425
Subroutines can be declared with multiple signatures.
421426
422427
multi sub foo() { say "Hello!" }
@@ -427,18 +432,60 @@ Inside of a class, you can also declare multi-dispatch methods.
427432
multi method greet { }
428433
multi method greet(Str $name) { }
429434
430-
=head2 Subroutine calls
435+
=head1 Subroutine calls
431436
432437
See L<functions|/language/functions>.
433438
434439
=comment TODO
435440
436441
foo; # Invoke the function foo with no arguments
437442
foo(); # Invoke the function foo with no arguments
438-
$f(); # Invoke $f, which contains a function
443+
&f(); # Invoke &f, which contains a function
439444
440445
=head1 Operators
441446
442-
See L<Operators|/language/operators>.
447+
See L<Operators|/language/operators> for lots of details.
448+
449+
Operators are functions with a more symbol heavy and composable syntax. Like
450+
other functions, operators can be multi-dispatch to allow for context-specific
451+
usage.
452+
453+
There are five types (arrangements) for operators, each taking either one or two arguments.
454+
455+
++$x # prefix, operator comes before single input
456+
5 + 3 # infix, operator is between two inputs
457+
$x++ # postfix, operator is after single input
458+
<the blue sky> # circumfix, operator surrounds single input
459+
%foo<bar> # postcircumfix, operator comes after first input and surrounds second
460+
461+
=head2 Meta Operators
462+
463+
Operators can be composed. A common example of this is combining an infix
464+
(binary) operator with assignment. You can do this with any binary operator.
465+
466+
$x += 5 # Adds 5 to $x, same as $x = $x + 5
467+
$x min= 3 # Sets $x to the smaller of $x and 3, same as $x = $x min 3
468+
469+
Wrap an infix operator in C<[ ]> to create a new reduction operator that works
470+
on a single list of inputs, resulting in a single value.
471+
472+
[+] <1 2 3 4 5> # 15
473+
(((1 + 2) + 3) + 4) + 5 # equivalent expanded version
474+
475+
Wrap an infix operator in C<« »> (or the ASCII equivalent C<<< >>>) to create a
476+
new hyper operator that works pairwise on two lists.
477+
478+
<1 2 3> «+» <4 5 6> # <5 7 9>
479+
480+
The direction of the arrows indicates what to do when the lists are not the same size.
481+
482+
@a «+« @b # Result is the size of @b, elements from @a will be re-used
483+
@a »+» @b # Result is the size of @a, elements from @b will be re-used
484+
@a «+» @b # Result is the size of the biggest input, the smaller one is re-used
485+
@a »+« @b # Exception if @a and @b are different sizes
486+
487+
You can also wrap a unary operator with a hyper operator.
488+
489+
-« <1 2 3> # <-1 -2 -3>
443490
444491
=end pod

0 commit comments

Comments
 (0)