/
operators.pod
1898 lines (1224 loc) · 55 KB
/
operators.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=begin pod
=TITLE Operators
=SUBTITLE Common Perl 6 infixes, prefixes, postfixes, and more!
=head1 Operator Precedence
In an expression like C<1 + 2 * 3>, the C<2 * 3> is evaluated first
because the infix C<*> has tighter B<precedence> than the C<+>.
The following table summarizes the precedence levels in Perl 6, from
tightest to loosest:
=begin table
A Level Examples
= ===== ========
N Terms 42 3.14 "eek" qq["foo"] $x :!verbose @$array
L Method postfix .meth .+ .? .* .() .[] .{} .<> .«» .:: .= .^ .:
N Autoincrement ++ --
R Exponentiation **
L Symbolic unary ! + - ~ ? | || +^ ~^ ?^ ^
L Multiplicative * / % %% +& +< +> ~& ~< ~> ?& div mod gcd lcm
L Additive + - +| +^ ~| ~^ ?| ?^
L Replication x xx
X Concatenation ~
X Junctive and &
X Junctive or | ^
L Named unary temp let
N Structural infix but does <=> leg cmp .. ..^ ^.. ^..^
C Chaining infix != == < <= > >= eq ne lt le gt ge ~~ === eqv !eqv
X Tight and &&
X Tight or || ^^ // min max
R Conditional ?? !! ff fff
R Item assignment = => += -= **= xx= .=
L Loose unary so not
X Comma operator , :
X List infix Z minmax X X~ X* Xeqv ...
R List prefix print push say die map substr ... [+] [*] any Z=
X Loose and and andthen
X Loose or or xor orelse
X Sequencer <==, ==>, <<==, ==>>
N Terminator ; {...}, unless, extra ), ], }
=end table
Using two C<!> symbols below generically to represent any pair of operators
that have the same precedence, the associativities specified above
for binary operators are interpreted as follows:
=begin table
A Assoc Meaning of $a ! $b ! $c
= ===== =======================
L left ($a ! $b) ! $c
R right $a ! ($b ! $c)
N non ILLEGAL
C chain ($a ! $b) and ($b ! $c)
X list infix:<!>($a; $b; $c)
=end table
For unary operators this is interpreted as:
=begin table
A Assoc Meaning of !$a!
= ===== =========================
L left (!$a)!
R right !($a!)
N non ILLEGAL
=end table
In the operator descriptions below, a default associativity of I<left>
is assumed.
=head1 Operator classification
Operators can occur in several positions relative to a term:
=begin table
+term prefix
term1 + term2 infix
term++ postfix
(term) circumfix
term1[term2] postcircumfix
=end table
Each operator is also available as a subroutine.
The name of the routine is formed of
the operator category, then a colon, and a list quote construct with the
symbol(s) that make up the operator:
infix:<+>(1, 2) # same as 1 + 2
circumfix:«( )»('a', 'b', 'c') # same as ('a', 'b', 'c')
As a special case the I<listop> (list operator) can stand either as a
term or as a prefix. Subroutine calls are the most common listops. Other
cases include meta-reduced infix operators (C<[+]| 1, 2, 3>) and the
L<#prefix ...> etc. stub operators.
Defining custom operators is covered in
L</language/functions#Defining_Operators>.
=head1 Meta Operators
Meta operators can be parameterized with other operators in the same way as
functions can take functions as parameters. Perl 6 will generate the actual
combined operator in the background, allowing the mechanism to be applied
to user defined operators. There are quite a few Meta operators with
different semantics, as explained in detail as follows.
=head2 Assignment Operators
Infix operators can be combined with the assignment operator to modify a
value and apply the result to a container in one go. Containers will be
autovivified if possible.
my $a = 32;
$a += 10; # 42
my $a = 3;
$a min= 5; # still 3
$a min= 2; # 2
Z<blocked by #63642 my @a = 1,2; @a ,= 3,4;>
This behavior is automatically extended to include custom-defined infix operators.
sub infix:<space-concat> ($a,$b) { $a ~ " " ~ $b };
my $a = 'word1';
$a space-concat= 'word2'; # 'word1 word2'
Although not strictly operators, methods can be used in the same fashion.
my Rat $a .= new;
my $a = 3.14;
$a .= Int; # 3
=head2 Negated Relational Operators
The result of a relational operator returning C<Bool> can be negated by
prefixing with C<!>. To avoid visual confusion with the C<!!> operator,
you may not modify any operator already beginning with C<!>.
There are shortcuts for C<!==> and C<!eq>, namely C<!=> and C<ne>.
my $a = True;
say so $a != True; # False
my $i = 10;
my $release = Date.new(:2015year, :12month, :24day);
my $today = Date.today;
say so $release !before $today; # True
=head2 Reversed Operators
Any infix operator may be called with its two arguments reversed by prefixing
with C<R>. Associativity of operands is reversed as well.
say 4 R/ 12; # 3
say [R/] 2, 4, 16; # 2
=head2 Hyper Operators
Hyper operators apply a given operator enclosed by C<«> and C<»> to one or two
lists, returning the resulting list. The pointy part of C<«> or C<»> has to
point to the shorter list. A list with just one element is fine too. If one of
the lists is shorter then the other, the operator will cycle over the shorter
list until all elements of the longer list are processed.
say (1,2,3) »*» 2; # (2,4,6)
say (1,2,3,4) »~» <a b>; # (1a 2b 3a 4b)
say (1,2,3) »+« (4,5,6); # (5 7 9)
Assignment meta operators can be hyped.
my @a = 1,2,3;
say @a »+=» 1; # [2 3 4]
Hyper forms of unary operators have the pointy bit point to the operator and
the blunt end at the list to be operated on.
my @wisdom = True, False, True;
say !« @wisdom; # [False True False]
my @a = 1,2,3;
@a»++; # (2,3,4)
Hyper operators are defined recursively on nested arrays.
say -« [[1, 2], 3]; # [[-1 -2] -3]
Methods can be called too, in an out of order, concurrent fashion. The resulting
list is in order. Please note that all hyper operators are candidates for
autothreading and will cause tears if said methods have side effects. The
optimizer has full reign over hyper operators, which is the reason that they
can not be defined by the user.
my CarefulClass @objs;
my @results = @objs».take-care();
my @slops; # May Contain Nuts
@slops».?this-method-may-not-exist();
Hyper operators can work with hashes. The pointy direction indicates if missing
keys are to be ignored in the resulting hash. The enclosed operator operates on
all values that have keys in both hashes.
=begin table
C<%foo «+» %bar;> intersection of keys
C<%foo »+« %bar;> union of keys
C<%outer »+» %inner;> only keys of %inner that exist in %outer will occur in the result
=end table
my %outer = 1,2,3 Z=> <a b c>;
my %inner = 1,2 Z=> <x z>;
say %outer «~» %inner; # {"1" => "ax", "2" => "bz"}
Hyper operators can take user defined operators as its operator argument.
sub pretty-file-site (Int $size --> Str) {
# rounding version of infix:</>(Int,Int)
sub infix:<r/>(Int \i1, Int \i2) {
round(i1 / i2, 0.1)
}
# we build a vector of fractions of $size and zip that with the fitting prefix
for $size «[r/]« (2**60, 2**50, 2**40, 2**30, 2**20, 2**10)
Z <EB PB TB GB MB KB> -> [\v,\suffix] {
# starting with the biggest suffix, we take the first that is 0.5 of that suffix or bigger
return v ~ ' ' ~ suffix if v > 0.4
}
# this be smaller or equal then 0.4 KB
return $size.Str;
}
for 60, 50, 40, 30, 20, 10 -> $test {
my &a = { (2 ** $test) * (1/4, 1/2, 1, 10, 100).pick * (1..10).pick };
print pretty-file-site(a.Int) xx 2, ' ';
}
# OUTPUT: «10 EB 4 EB 2 PB 5 PB 0.5 PB 4 TB 300 GB 4.5 GB 50 MB 200 MB 9 KB 0.6 MB»
Hyper operators do not descent into child lists. You can chain hyper operators
to destructure a List of Lists.
my $neighbors = ((-1, 0), (0, -1), (0, 1), (1, 0));
my $p = (2,3);
say $neighbors »>>+<<» ($p, *); # ((1 3) (2 2) (2 4) (3 3))
=head2 Reduction Operators
Reduction operators apply any infix operator, surrounded by C<[> and C<]>,
element by element and return the resulting value.
say [+] 1,2,3; # 6
They can be defined as a list prefix operators or will be generated automatically.
multi infix:<[~~]> (@c, &test) is looser(&infix:<~~>) {
gather for @c { take $_ if test($_) }
};
my @l = 1,'a',2,'b';
say @l [~~] {$^a ~~ Str}; # (a b)
For list infix operators, flattening is not done on the input list. This
allows list operators to become the reduction operator.
[X~] (1,2), <a b> # 1,2 X~ <a b>
By default reduction meta operators are eager. To lazily generate values,
prefix the operator with a C<\>. If the non-meta part contains a C<\> already,
quote it with C<[]> (e.g. C<[\[\x]]>).
my $lazy := [\+] 1..*;
say $lazy[^10]; # (1 3 6 10 15 21 28 36 45 55)
=head2 Cross Operators
The cross metaoperator, C<X>, will apply a given infix operator in order of
cross product to all lists, such that the rightmost operator varies most
quickly.
1..3 X~ <a b>
# produces <1a, 1b, 2a, 2b, 3a, 3b>
=head2 Zip Operators
The zip metaoperator, C<Z>, will apply a given infix operator to pairs taken
one left, one right, from its arguments. The resulting list is returned.
my @l = <a b c> Z~ 1,2,3; # [a1 b2 c3]
If one of the operands runs out of elements prematurely, the zip operator will
stop. An infinite list can be used to repeat elements. A list with a final
element of C<*> will repeat its 2nd last element indefinitely.
my @l = <a b c d> Z~ ':' xx *; # <a: b: c: d:>
my @l = <a b c d> Z~ 1, 2, *; # <a1 b2 c2 d2>
=head2 Sequential Operators
The sequential metaoperator, C<S>, will suppress any concurrency, or reordering
done by the optimizer. Most simple infix operators are supported.
say so 1 S& 2 S& 3; # True
=head2 Nesting of Meta Operators
To avoid ambiguity when chaining meta operators use square brackets to help the
compiler to understand you.
my @a = 1,2,3;
my @b = 5,6,7;
@a X[+=] @b;
say @a; # [19 20 21]
=head1 Z<>Term Precedence
=head2 circumfix C«< >»
The X<quote-words|qw; quote-words> construct. Breaks up the contents on whitespace, and returns
a L<List|/type/List> of the words. If a word
looks like a number literal or a C<Pair> literal, it is converted to the
appropriate number.
say <a b c>[1]; # b
=head2 circumfix C«( )»
The X<grouping operator>.
An empty group C<()> creates an empty L<List>.
Parens around non-empty expressions simply structure the expression, but
not have additional semantics.
In an argument list, putting parenthesis around an argument prevents it from
being interpreted as a named argument.
multi sub p(:$a!) { say 'named' }
multi sub p($a) { say 'positional' }
p a => 1; # named
p (a => 1); # positional
=head2 circumfix C«{ }»
Block or L<Hash> constructor.X<|block constructor; hash constructor>
If the contents looks like a list of pairs and does not use L<$_> or other
placeholder parameters, returns an itemized L<Hash>.
Otherwise it constructs a L<Block>.
Note that this construct does not re-parse the contents; rather the
contents are always parsed as a statement list (i.e. like a block),
and if the later analysis shows that it needs to be interpreted as a hash,
the block is executed and coerced to L<Hash>.
=head2 circumfix C«[ ]»
The X<L<Array> constructor>. Returns an itemized L<Array> which does not flatten
in list context.
=head1 Method Postfix Precedence
=head2 postcircumfix C«[ ]»
sub postcircumfix:<[ ]>(@container, **@index,
:$k, :$v, :$kv, :$p, :$exists, :$delete)
Universal interface for positional access to zero or more elements of a
@container, a.k.a. "X<array indexing operator|array indexing operator; array subscript operator>".
my @alphabet = 'a' .. 'z';
say @alphabet[0]; #-> a
say @alphabet[1]; #-> b
say @alphabet[*-1]; #-> z
say @alphabet[100]:exists; #-> False
say @alphabet[15, 4, 17, 11].join; #-> perl
say @alphabet[23 .. *].perl; #-> ("x", "y", "z")
@alphabet[1, 2] = "B", "C";
say @alphabet[0..3].perl #-> ("a", "B", "C", "d")
See L<Subscripts|/language/subscripts> for a more detailed explanation of this
operator's behavior, and how to implement support for it in custom types.
=head2 postcircumfix C«{ }»
sub postcircumfix:<{ }>(%container, **@key,
:$k, :$v, :$kv, :$p, :$exists, :$delete)
Universal interface for associative access to zero or more elements of a
%container, a.k.a. "X<hash indexing operator|hash indexing operator; hash subscript operator>".
my %color = kiwi => "green", banana => "yellow", cherry => "red";
say %color{"banana"}; #-> yellow
say %color{"cherry", "kiwi"}.perl; #-> ("red", "green")
say %color{"strawberry"}:exists; #-> False
%color{"banana", "lime"} = "yellowish", "green";
%color{"cherry"}:delete;
say %color; #-> banana => yellowish, kiwi => green, lime => green
See L«C«postcircumfix < >»|/routine/[ ]#postcircumfix_<_>» and
L<C<postcircumfix « »>|/routine/[ ]#postcircumfix_«_»> for convenient
shortcuts, and L<Subscripts|/language/subscripts> for a more detailed
explanation of this operator's behavior, and how to implement support for it
in custom types.
=head2 postcircumfix C«< >»
Shortcut for L<C<postcircumfix { }>|/routine/[ ]#postcircumfix_{_}> that quotes
its argument using the same rules as the L«quote-words operator|
/routine/< >#circumfix_<_>» of the same name.
my %color = kiwi => "green", banana => "yellow", cherry => "red";
say %color<banana>; #-> yellow
say %color<cherry kiwi>.perl; #-> ("red", "green")
say %color<strawberry>:exists; #-> False
This is not a real operator, just syntactic sugar that is turned into the
C<{ }> postcircumfix operator at compile-time.
=head2 postcircumfix C<« »>
Shortcut for L<C<postcircumfix { }>|/routine/[ ]#postcircumfix_{_}> that quotes
its argument using the same rules as the L<interpolating quote-words operator|
/routine/« »#circumfix_«_»> of the same name.
my %color = kiwi => "green", banana => "yellow", cherry => "red";
my $fruit = "kiwi";
say %color«cherry $fruit».perl; #-> ("red", "green")
This is not a real operator, just syntactic sugar that is turned into the
C<{ }> postcircumfix operator at compile-time.
=head2 postcircumfix C«( )»
The X<call operator>. Treats the invocant as a L<Callable> and invokes it,
using the expression between the parens as arguments.
Note that an identifier followed by a pair of parens is always parsed as a
subroutine call.
If you want your objects to respond to the call operator, you need to
implement a C<method CALL-ME>.
=head2 postfix C«.»
The operator for calling one method, C<$invocant.method>.X<|method call>
Technically this is not an operator, but syntax special-cased in the compiler.
=head2 postfix C«.=»
A X<mutating method call>. C<$invocant.=method> desugars to
C<$invocant = $invocant.method>, similar to L<C<op=>>.
Technically this is not an operator, but syntax special-cased in the compiler.
=head2 postfix C«.^»
A X<meta-method call>. C<$invocant.^method> calls C<method> on C<$invocant>'s
metaclass. It desugars to C<$invocant.HOW.method($invocant, ...)>. See L<C<HOW>>
for more information.
Technically this is not an operator, but syntax special-cased in the compiler.
=head2 postfix C«.?»
X<Potential method call>s. C<$invocant.?method> calls method C<method> on
C<$invocant> if it has a method of such name. Otherwise it returns L<Nil>.
Technically this is not an operator, but syntax special-cased in the compiler.
=head2 postfix C«.+»
C<$invocant.+method> calls all methods called C<method> from C<$invocant>,
and returns a L<List> of the results. Dies if no such method was found.
Technically this is not an operator, but syntax special-cased in the compiler.
=head2 postfix C«.*»
C<$invocant.*method> calls all methods called C<method> from C<$invocant>,
and returns a L<List> of the results. If no such method was found, an empty
L<List> is returned.
Technically this is not an operator, but syntax special-cased in the compiler.
=head2 postfix C<».> / postfix C«>>.»
X<Hyper method call operator>. Will call a method on all elements of a C<List> out of order and return the list of return values in order.
my @a = <a b c>;
@a».say; # abc
my @b = @a».ord; # [97, 98, 99]
sub foo(Str:D $c){ $c.ord * 2 }; # The first parameter of a method is the invocant.
say @a».&foo; # So we can pretend to have a method call with a sub that got a good first positional argument.
say @a».&({ .ord}); # Blocks have an implicit positional arguments that lands in $_. The latter can be obmitted for method calls.
=head2 X«postfix C<.postfix>
|postfix,.postfix;postcircumfix,.( );postcircumfix,.[ ];postcircumfix,.{ };postcircumfix,.< >»
In most cases, a dot may be placed before a postfix or postcircumfix:
@a[1, 2, 3];
@a.[1, 2, 3]; # Same
This can be useful for visual clarity or brevity. For example, if an object's
attribute is a function, putting a pair of parentheses after the attribute name
will become part of the method call. So either two pairs of parentheses must be
used, or a dot has to come before the parentheses to separate it from the method
call.
class Operation {
has $.symbol;
has &.function;
}
my $addition = Operation.new(:symbol<+>, :function{ $^a + $^b });
say $addition.function()(1, 2); # 3
# OR
say $addition.function.(1,2); # 3
If the postfix is an identifier, however, it will be interpreted as a normal
method call.
1.i # No such method 'i' for invocant of type 'Int'
Technically this is not an operator, but syntax special-cased in the compiler.
X<|postfix call>
=head2 postfix C«.:<prefix>»
A prefix can be called like a method using colonpair notation. For example:
my $a = 1;
say ++$a; # 2
say $a.:<++>; # 3
Technically this is not an operator, but syntax special-cased in the compiler.
X<|prefix call>
=head2 postfix C«.::»
A X<class-qualified method call>, used to call a method as defined in a parent
class or role, even after it has been redefined in the child class.
class Bar {
method baz { 42 }
}
class Foo is Bar {
method baz { "nope" }
}
say Foo.Bar::baz; # 42
=head1 Autoincrement Precedence
=head2 prefix C«++»
multi sub prefix:<++>($x is rw) is assoc<none>
Increments its argument by one, and returns the incremented value.X<|increment operator>
my $x = 3;
say ++$x; # 4
say $x; # 4
It works by calling the L<succ> method (for I<successor>) on its argument,
which gives custom types the freedom to implement their own increment
semantics.
=head2 prefix C«--»
multi sub prefix:<-->($x is rw) is assoc<none>
Decrements its argument by one, and returns the decremented value.X<|decrement operator>
my $x = 3;
say --$x; # 2
say $x; # 2
It works by calling the L<pred> method (for I<predecessor>) on its argument,
which gives custom types the freedom to implement their own decrement
semantics.
=head2 postfix C«++»
multi sub postfix:<++>($x is rw) is assoc<none>
Increments its argument by one, and returns the unincremented value.X<|increment operator>
my $x = 3;
say $x++; # 3
say $x; # 4
It works by calling the L<succ> method (for I<successor>) on its argument,
which gives custom types the freedom to implement their own incrementation
semantics.
Note that this does not necessarily return its argument. For example for
undefined values, it returns 0:
my $x;
say $x++; # 0
say $x; # 1
=head2 postfix C«--»
multi sub postfix:<-->($x is rw) is assoc<none>
Decrements its argument by one, and returns the undecremented value.X<|decrement operator>
my $x = 3;
say $x--; # 3
say $x; # 2
It works by calling the L<pred> method (for I<predecessor>) on its argument,
which gives custom types the freedom to implement their own decrementation
semantics.
Note that this does not necessarily return its argument. For example for
undefined values, it returns 0:
my $x;
say $x--; # 0
say $x; # -1
=head1 Exponentiation Precedence
=head2 infix C«**»
multi sub infix:<**>(Any, Any) returns Numeric:D is assoc<right>
The X<exponentiation operator> coerces both arguments to L<Numeric>
and calculates the left-hand-side raised to the power of the right-hand side.
If the right-hand side is a non-negative integer and the left-hand side
is an arbitrary precision type (L<Int>, L<FatRat>), then the calculation
is carried out without loss of precision.
=head1 Symbolic Unary Precedence
=head2 prefix C«?»
multi sub prefix:<?>(Mu) returns Bool:D
X<Boolean context operator>.
Coerces the argument to L<Bool> by calling the C<Bool> method on it.
Note that this collapses L<Junction>s.
=head2 prefix C«!»
multi sub prefix:<!>(Mu) returns Bool:D
X<Negated boolean context operator>.
Coerces the argument to L<Bool> by calling the C<Bool> method on it,
and returns the negation of the result.
Note that this collapses L<Junction>s.
=head2 prefix C«+»
multi sub prefix:<+>(Any) returns Numeric:D
X<Numeric context operator>.
Coerces the argument to L<Numeric> by calling the C<Numeric> method on it.
=head2 prefix C«-»
multi sub prefix:<->(Any) returns Numeric:D
X<Negative numeric context operator>.
Coerces the argument to L<Numeric> by calling the C<Numeric> method on it,
and then negates the result.
=head2 prefix C«~»
multi sub prefix:<->(Any) returns Str:D
X<String context operator>.
Coerces the argument to L<Str> by calling the C<Str> method on it.
=head2 prefix C«|»
Flattens objects of type L<Capture>, L<Pair>, L<List> L<Map> and L<Hash>
into an argument list.
Outside of argument lists, it returns a L<Slip|/type/Slip>, which makes it
flatten into the outer list.
=head2 prefix C«+^»
multi sub prefix:<+^>(Any) returns Int:D
X<Integer bitwise negation operator>.
Coerces the argument to L<Int> and does a bitwise negation on the result,
assuming L<two's complement|https://en.wikipedia.org/wiki/Two%27s_complement>.
=head2 prefix C«?^»
multi sub prefix:<?^>(Mu) returns Bool:D
X<Boolean bitwise negation operator>.
Coerces the argument to L<Bool> and then does a bit flip, which makes it the
same as C<< prefix:<!> >>.
=head2 prefix C«^»
multi sub prefix:<^>(Any) returns Range:D
I<upto> operator.X<|upto operator>
Coerces the argument to L<Numeric>, and generates a range from 0 up to (but
excluding) the argument.
say ^5; # 0..^5
for ^5 { } # 5 iterations
=head1 Multiplicative Precedence
=head2 infix C«*»
multi sub infix:<*>(Any, Any) returns Numeric:D
X<Multiplication operator>.
Coerces both arguments to L<Numeric> and multiplies them. The result
is of the wider type. See L<Numeric> for details.
=head2 infix C«/»
multi sub infix:</>(Any, Any) returns Numeric:D
X<Division operator>.
Coerces both argument to L<Numeric> and divides the left through the right
number. Division of L<Int> values returns L<Rat>, otherwise the "wider type"
rule described in L<Numeric> holds.
=head2 infix C«div»
multi sub infix:<div>(Int:D, Int:D) returns Int:D
X<Integer division operator>. Rounds down.
=head2 infix C«%»
multi sub infix:<%>($x, $y) return Numeric:D
X<Modulo operator>. Coerces to L<Numeric> first.
Generally the following identity holds:
$x % $y == $x - floor($x / $y) * $y
=head2 infix C«%%»
multi sub infix:<%%>($a, $b) returns Bool:D
X<Divisibility operator>. Returns C<True> if C<$a % $b == 0>.
=head2 infix C«mod»
multi sub infix:<mod>(Int:D $a, Int:D $b) returns Int:D
X<Integer modulo operator>. Returns the remainder of an integer modulo operation.
=head2 infix C«+&»
multi sub infix:<+&>($a, $b) returns Int:D
Numeric bitwise I<AND> operator. Coerces both arguments to L<Int> and does a bitwise
I<AND> operation assuming two's complement.X<|Numeric bitwise AND operator>
=head2 infix C«+<»
multi sub infix:<< +< >>($a, $b) returns Int:D
Integer bit shift to the left.X<|integer bit shift operator, left>
=head2 infix C«+>»
multi sub infix:<< +> >>($a, $b) returns Int:D
Integer bit shift to the right.X<|integer bit shift operator, right>
=head2 infix C«gcd»
multi sub infix:<gcd>($a, $b) returns Int:D
Coerces both arguments to L<Int> and returns the greatest common denominator.X<|greatest commmon demnominator operator>
=head2 infix C«lcm»
multi sub infix:<lcm>($a, $b) returns Int:D
Coerces both arguments to L<Int> and returns the least common multiple, that is
the smallest integer that is evenly divisible by both arguments.X<|least common multiple operator>
=head1 Additive Precedence
=head2 infix C«+»
multi sub infix:<+>($a, $b) returns Numeric:D
X<Addition operator>.
Coerces both arguments to L<Numeric> and adds them.
=head2 infix C«-»
multi sub infix:<->($a, $b) returns Numeric:D
X<Subtraction operator>.
Coerces both arguments to L<Numeric> and subtracts the second from the
first.
=head2 infix C«+|»
multi sub infix:<+|>($a, $b) returns Int:D
X<Integer bitwise OR operator>.
Coerces both arguments to L<Int> and does a bitwise I<OR> (inclusive OR)
operation.
=head2 infix C«+^»
multi sub infix:<+^>($a, $b) returns Int:D
X<Integer bitwise XOR operator>.
Coerces both arguments to L<Int> and does a bitwise I<XOR> (exclusive OR)
operation.
=head2 infix C«?|»
multi sub infix:<?|>($a, $b) returns Bool:D
X<Boolean logical OR operator>.
Coerces both arguments to L<Bool> and does a logical I<OR> (inclusive OR)
operation.
=head1 Replication Precedence
=head2 infix C«x»
proto sub infix:<x>(Any, Any) returns Str:D
multi sub infix:<x>(Any, Any)
multi sub infix:<x>(Str:D, Int:D)
X<String repetition operator>.
Coerces C<$a> to L<Str> and C<$b> to L<Int> and repeats the string C<$b>
times. Return the empty string if C<< $b <= 0 >>.
say 'ab' x 3; # ababab
say 42 x 3; # 424242
=head2 infix C«xx»
multi sub infix:<xx>($a, $b) returns List:D
X<List repetition operator>.
Returns a list of C<$a> repeated and evaluated C<$b> times (C<$b> is coerced
to L<Int>). If C<< $b <= 0 >>, the empty list is returned.
The left-hand side is evaluated for each repetition, so
[1, 2] xx 5
returns five distinct arrays (but with the same content each time), and
rand xx 3
returns three pseudo random numbers that are determined independently.
The right-hand side can be C<*>, in which case a lazy, infinite list
is returned.
=head1 Concatenation
=head2 infix C«~»
proto sub infix:<~>(Any, Any) returns Str:D
multi sub infix:<~>(Any, Any)
multi sub infix:<~>(Str:D, Str:D)
X<String concatenation operator>.
Coerces both arguments to L<Str> and concatenates them.
say 'ab' ~ 'c'; # abc
=head1 Junctive AND (all) Precedence
=head2 infix C«&»
multi sub infix:<&>($a, $b) returns Junction:D is assoc<list>
X<All junction operator>.
Creates an I<all> L<Junction> from its arguments. See L<Junction> for more
details.
=head1 Junctive OR (any) Precedence
=head2 infix C«|»
multi sub infix:<|>($a, $b) returns Junction:D is assoc<list>
X<Any junction operator>.
Creates an I<any> L<Junction> from its arguments. See L<Junction> for more
details.
=head2 infix C«^»
multi sub infix:<^>($a, $b) returns Junction:D is assoc<list>
X<One junction operator>.
Creates a I<one> L<Junction> from its arguments. See L<Junction> for more
details.
=head1 Named Unary Precedence
=head2 prefix C«temp»
sub prefix:<temp>(Mu $a is rw)
"temporizes" the variable passed as the argument, which means it is reset
to its old value on scope exit. (This is similar to the
L<local|http://perldoc.perl.org/functions/local.html> operator in Perl 5,
except that C<temp> does not reset the value).
=head2 prefix C«let»
sub prefix:<let>(Mu $a is rw)
Restores the previous value if the block exits unsuccessfully. A
successful exit means the block returned a defined value or a list.
my $answer = 42;
{
let $answer = 84;
die if not Bool.pick;
CATCH {
default { say "it's been reset :(" }
}
say "we made it 84 sticks!";
}
say $answer;
In the above case, if the C<Bool.pick> returns true, the answer will
stay as 84 because the block returns a defined value (C<say> returns
true). Otherwise the C<die> statement will cause the block to exit
unsuccessfully, resetting the answer to 42.
=comment this is duplicated in variables.pod
=head1 Nonchaining Binary Precedence
=head2 infix C«does»
sub infix:<does>(Mu $obj, Mu $role) is assoc<none>
Mixes C<$role> into C<$obj> at run time. Requires C<$obj> to be mutable.
C<$role> doesn't need to a be a role, it can be something that knows how
to act like a role, for example enum values.
=head2 infix C«but»
sub infix:<but>(Mu $obj, Mu $role) is assoc<none>
Creates a copy of C<$obj> with C<$role> mixed in. Since C<$obj> is not