Skip to content

Commit 92d045b

Browse files
committed
Document Nil → Any trap closes #1134
Uses also #1135 example to document it.
1 parent 0927fc5 commit 92d045b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

doc/Language/traps.pod6

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ a behaviour similar to a constant, but allowing the value to get updated:
4343
unit module Something::Or::Other;
4444
my $config-file := "config.txt".IO.slurp;
4545
46+
=head2 Assigning to C<Nil> produces a different value, usually C<Any>.
47+
48+
Actually, assigning to C<Nil> L<reverts the variable to its default value|https://docs.perl6.org/type/Nil>. So:
49+
50+
=begin code
51+
my @a = 4, 8, 15, 16;
52+
@a[2] = Nil;
53+
say @a; # OUTPUT: «[4 8 (Any) 16]␤»
54+
=end code
55+
56+
In this case, C<Any> is the default value of an C<Array> element.
57+
58+
You can purposefully assign C<Nil> as a default value:
59+
60+
=begin code
61+
my %h is default(Nil) = a => Nil;
62+
dd %h; # OUTPUT: «Hash %h = {:a(Nil)}␤»
63+
=end code
64+
65+
Or bind a value to C<Nil> if that is the result you want:
66+
67+
=begin code
68+
@a[3] := Nil;
69+
say @a; # OUTPUT: «[4 8 (Any) Nil]␤»
70+
=end code
71+
72+
This trap might be hidden in the result of functions, such as matches:
73+
74+
=begin code
75+
my $result2 = 'abcdef' ~~ / dex /;
76+
say "Result2 is { $result2.^name }"; # OUTPUT: «Result2 is Any␤»
77+
=end code
78+
79+
A L<C<Match> will be C<Nil>|https://docs.perl6.org/language/regexes#Literals> if it finds nothing; however it assigning C<Nil> to C<$result2> above will result in its default value, which is C<Any> as shown.
80+
4681
=head1 Blocks
4782
4883
=head2 Beware of empty "blocks"

0 commit comments

Comments
 (0)