Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reflows while revising for #1265
  • Loading branch information
JJ committed Mar 21, 2019
1 parent 62c7f85 commit 33dafa9
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions doc/Language/traps.pod6
Expand Up @@ -146,7 +146,8 @@ instead:
# Count is 1
=end code
Alternatively, you can also use the L<concatenation operator|/routine/~> instead:
Alternatively, you can also use the L<concatenation operator|/routine/~>
instead:
=begin code
sub count-it { say "Count is " ~ $++ }
Expand All @@ -159,23 +160,23 @@ L<(elem)|/routine/(elem)>, L<∈|/routine/∈>, or L<∉|/routine/∉> on classe
implementing L<Associative|/type/Associative> will return C<False> if the value
of the key is falsy:
=begin code
enum Foo «a b»;
say Foo.enums ∋ 'a';
=begin code
enum Foo «a b»;
say Foo.enums ∋ 'a';
# OUTPUT:
# False
=end code
# OUTPUT:
# False
=end code
Instead, use C<:exists>:
=begin code
enum Foo «a b»;
say Foo.enums<a>:exists;
=begin code
enum Foo «a b»;
say Foo.enums<a>:exists;
# OUTPUT:
# True
=end code
# OUTPUT:
# True
=end code
=head1 Blocks
Expand Down Expand Up @@ -223,8 +224,8 @@ say Point.new(x => 1, y => -2).double.x
# OUTPUT: «Cannot assign to an immutable value␤»
=end code
the first line inside the method C<double> is marked with C<# WRONG> because C<$.x>,
short for C<$( self.x )>, is a call to a read-only accessor.
the first line inside the method C<double> is marked with C<# WRONG> because
C<$.x>, short for C<$( self.x )>, is a call to a read-only accessor.
The syntax C<has $.x> is short for something like C<has $!x; method x() {
$!x }>, so the actual attribute is called C<$!x>, and a read-only accessor
Expand All @@ -241,7 +242,8 @@ method double {
which operates on the attributes directly.
=head2 C<BUILD> prevents automatic attribute initialization from constructor arguments
=head2 C<BUILD> prevents automatic attribute initialization from constructor
arguments
When you define your own C<BUILD> submethod, you must take care of
initializing all attributes by yourself. For example
Expand Down Expand Up @@ -363,9 +365,10 @@ value when that C<Capture> is later used. For example:
my $a = 2; say join ",", ($a, ++$a); # OUTPUT: «3,3␤»
Here the C<Capture> contained the B<container> pointed to by C<$a> and the
B<value> of the result of the expression C<++$a>. Since the C<Capture> must
be reified before C<&say> can use it, the C<++$a> may happen before C<&say>
looks inside the container in C<$a> (and before the C<List> is created with the two terms) and so it may already be incremented.
B<value> of the result of the expression C<++$a>. Since the C<Capture> must be
reified before C<&say> can use it, the C<++$a> may happen before C<&say> looks
inside the container in C<$a> (and before the C<List> is created with the two
terms) and so it may already be incremented.
Instead, use an expression that produces a value when you want a value.
Expand All @@ -387,7 +390,11 @@ for ^5 {
};
=end code
Outputs C<«[(1 2)]␤[(2 3) (2 3)]␤[(3 5) (3 5) (3 5)]␤...>. C<$a> and C<$b> are not reified until C<say> is called, the value that they have in that precise moment is the one printed. To avoid that, decontainerize values or take them out of the variable in some way before using them.
Outputs C<«[(1 2)]␤[(2 3) (2 3)]␤[(3 5) (3 5) (3 5)]␤...>. C<$a> and C<$b> are
not reified until C<say> is called, the value that they have in that precise
moment is the one printed. To avoid that, decontainerize values or take them out
of the variable in some way before using them.
=begin code
my @arr;
my ($a, $b) = (1,1);
Expand All @@ -405,15 +412,17 @@ value extracted, and the desired outcome achieved.
Perl 6 includes a L<Cool|/type/Cool> class, which provides some of the DWIM
behaviors we got used to by coercing arguments when necessary. However, DWIM is
never perfect. Especially with L<List|/type/List>s, which are C<Cool>, there are many
methods that will not do what you probably think they do, including C<contains>,
C<starts-with> or C<index>. Please see some examples in the section below.
never perfect. Especially with L<List|/type/List>s, which are C<Cool>, there are
many methods that will not do what you probably think they do, including
C<contains>, C<starts-with> or C<index>. Please see some examples in the section
below.
=head2 Strings are not C<List>s, so beware indexing
In Perl 6, L<strings|/type/Str> are not lists of characters. One
L<cannot iterate|#Strings_are_not_iterable> over them or index into them as you can
with L<lists|/type/List>, despite the name of the L<.index routine|/type/Str#routine_index>.
L<cannot iterate|#Strings_are_not_iterable> over them or index into them as you
can with L<lists|/type/List>, despite the name of the L<.index
routine|/type/Str#routine_index>.
=head2 C<List>s become strings, so beware C<.index()>ing
Expand Down Expand Up @@ -495,11 +504,16 @@ You want to check whether a number is divisible by any of a set of numbers:
say 42 %% <11 33 88 55 111 20325>; # OUTPUT: «True␤»
What? There's no single number 42 should be divisible by. However, that list has 6 elements, and 42 is divisible by 6. That's why the output is true. In this case, you should turn the C<List> into a L<Junction|/type/Junction>:
What? There's no single number 42 should be divisible by. However, that list has
6 elements, and 42 is divisible by 6. That's why the output is true. In this
case, you should turn the C<List> into a L<Junction|/type/Junction>:
say 42 %% <11 33 88 55 111 20325>.any; # OUTPUT: «any(False, False, False, False, False, False)␤»
=for code
say 42 %% <11 33 88 55 111 20325>.any;
# OUTPUT: «any(False, False, False, False, False, False)␤»
which will clearly reveal the falsehood of the divisiveness of all the numbers in the list, which will be numified separately.
which will clearly reveal the falsehood of the divisiveness of all the numbers
in the list, which will be numified separately.
=head1 Arrays
Expand Down

0 comments on commit 33dafa9

Please sign in to comment.