You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Language/traps.pod6
+35Lines changed: 35 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,41 @@ a behaviour similar to a constant, but allowing the value to get updated:
43
43
unit module Something::Or::Other;
44
44
my $config-file := "config.txt".IO.slurp;
45
45
46
+
=head2Assigning 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
+
=begincode
51
+
my @a = 4, 8, 15, 16;
52
+
@a[2] = Nil;
53
+
say @a; # OUTPUT: «[4 8 (Any) 16]»
54
+
=endcode
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
+
=begincode
61
+
my %h is default(Nil) = a => Nil;
62
+
dd %h; # OUTPUT: «Hash %h = {:a(Nil)}»
63
+
=endcode
64
+
65
+
Or bind a value to C<Nil> if that is the result you want:
66
+
67
+
=begincode
68
+
@a[3] := Nil;
69
+
say @a; # OUTPUT: «[4 8 (Any) Nil]»
70
+
=endcode
71
+
72
+
This trap might be hidden in the result of functions, such as matches:
73
+
74
+
=begincode
75
+
my $result2 = 'abcdef' ~~ / dex /;
76
+
say "Result2 is { $result2.^name }"; # OUTPUT: «Result2 is Any»
77
+
=endcode
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.
0 commit comments