Skip to content

Commit cb36b25

Browse files
committed
[operators] &&, ||, //, min, max, item =, =>, not, so
1 parent 0bf9571 commit cb36b25

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

lib/operators.pod

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,118 @@ Here is an excerpt of built-in smart-matching functionality:
806806
807807
=end table
808808
809+
=head1 Tight AND Precedence
810+
811+
=head2 infix &&
812+
813+
Returns the first argument that evaluates to C<False> in boolean context,
814+
or otherwise the last argument.
815+
816+
Note that this short-circuits, i.e. if one of the arguments evaluates to a
817+
false value, the arguments to the right of are never evaluated.
818+
819+
sub a { 1 }
820+
sub b { 0 }
821+
sub c { die "never called" };
822+
say a() && b() && c(); # 0
823+
824+
=head1 Tight OR Precedence
825+
826+
=head2 infix ||
827+
828+
Returns the first argument that evaluates to C<True> in boolean context,
829+
or otherwise the last argument.
830+
831+
Note that this short-circuits, i.e. if one of the arguments evaluates to a
832+
true value, the arguments to the right of are never evaluated.
833+
834+
sub a { 0 }
835+
sub b { 1 }
836+
sub c { die "never called" };
837+
say a() || b() || c(); # 1
838+
839+
=head2 infix ^^
840+
841+
Returns the first true argument if there is only one, and L<Nil> otherwise.
842+
Short-circuits as soon as two true arguments are found.
843+
844+
say 0 ^^ 42; # 42
845+
say 0 ^^ 42 ^^ 1 ^^ die 8; # (empty line)
846+
847+
=head2 infix //
848+
849+
Defined-or operator. Returns the first defined operand. Short-circuits.
850+
851+
say Any // 0 // 42; # 0
852+
853+
=head2 infix min
854+
855+
Returns the smallest of the arguments, as determined by L<cmp> semantics.
856+
857+
=head2 infix max
858+
859+
Returns the largest of the arguments, as determined by L<cmp> semantics.
860+
861+
=head1 Conditional Operator Precedence
862+
863+
=head2 infix ?? !!
864+
865+
Ternary operator, conditional operator.
866+
867+
C<$condition ?? $true !! $false> evaluates and returns the expression from the
868+
C<$true> branch if C<$condition> is a true value. Otherwise it evaluates and
869+
returns the C<$false> branch.
870+
871+
# TODO: ff, ^ff, ff^, ^ff^, fff, ^fff, fff^, ^fff^
872+
873+
=head1 Item Assignment Precedence
874+
875+
=head2 infix =
876+
877+
sub infix:<=>(Mu $a is rw, Mu $b)
878+
879+
Item assignment.
880+
881+
Places the value of the left-hand side into the container on the right-hand
882+
side.
883+
884+
(Note that item assignment and list assignment have different precedence
885+
levels, and the syntax of the left-hand side decides whether an equal sign
886+
C<=> is parsed as item assignment or list assignment operator).
887+
888+
=head2 infix =>
889+
890+
sub infix:«=>»($key, Mu $value) returns Pair:D
891+
892+
L<Pair> constructor.
893+
894+
Constructs a L<Pair> object with the left-hand side as the key and the
895+
right-hand side as the value.
896+
897+
Note that the C<< => >> operator is syntactically special-cased, in that
898+
it allows unquoted identifier on the left-hand side.
899+
900+
my $p = a => 1;
901+
say $p.key; # a
902+
say $p.value; # 1
903+
904+
A L<Pair> within an argument list with an unquoted identifier on the left
905+
is interpreted as a named argument.
906+
907+
=head1 Loose Unary Precedence
908+
909+
=head2 prefix not
910+
911+
multi sub prefix:<not>(Mu $x) returns Bool:D
912+
913+
Evaluates its argument in boolean context (and thus collapses L<Junction>s),
914+
and negates the result.
915+
916+
=head2 prefix so
917+
918+
multi sub prefix:<so>(Mu $x) returns Bool:D
919+
920+
Evaluates its argument in boolean context (and thus collapses L<Junction>s),
921+
and returns the result.
922+
809923
=end pod

0 commit comments

Comments
 (0)