Skip to content

Commit a47ed84

Browse files
committed
Explain convenience behaviors in associative subscripting section
(could use some links, but too tired) Also, provide an example of off-end access of a native array.
1 parent af677c4 commit a47ed84

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

lib/Language/subscripts.pod

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ allows arbitrary objects as keys, etc.:
4646
say %grade{"Ben"}; #-> B+
4747
4848
my $stats = ( Date.today => 4.18, Date.new(2015, 4, 5) => 17.253 ).Mix;
49-
say $stats{ Date.new(2015, 4, 5) }; #-> 17.253
49+
say $stats{ Date.new(2015, 4, 4) + 1 }; #-> 17.253
5050
5151
For passing single-word string keys to C<{ }>, you can also use the
5252
L<angle-bracketed word quoting constructs|/language/quoting#Word_quoting:_qw>
@@ -62,6 +62,40 @@ C<{ }> form at compile-time:
6262
%hash«foo $var»; # same as %hash{ «foo $var» }
6363
%hash<<foo $var>>; # same as %hash{ <<foo $var>> }
6464
65+
You may have noted above that we avoided having to quote C<Zoe> by using
66+
the C<=>> operator, but that same operator did not just put invisible
67+
quotes around C<Date.new(2015, 4, 5)>, and we were able to find the
68+
same element using C<$stats{ Date.new(2015, 4, 4) + 1 }>. This is because
69+
C<=>> only puts invisible quotes around single words, and by "word" we
70+
mean an identifier/name. The C<=>> operator is there to prevent us from
71+
accidentally calling functions or using constants with that name.
72+
73+
Hash subscripts do not do the same thing as C<=>>. The default C<Hash>
74+
has been made to behave the way new users have come to expect
75+
from using other languages, and for general ease of use. On a
76+
default C<Hash>, subscripts coerce keys into strings, as long as
77+
those keys produce something C<Cool>. You can use C<.perl> on a
78+
collection to be sure whether the keys are strings or objects:
79+
80+
( 1 => 1 ).perl.say #-> 1 => 1
81+
my %h; %h{1} = 1; say %h.perl; #-> { "1" => 1 }
82+
( 1/2 => 1 ).perl.say #-> 0.5 => 1
83+
my %h; %h{1/2} = 1; say %h.perl; #-> { "0.5" => 1 }
84+
( pi => 1 ).perl.say #-> :pi(1)
85+
my %h; %h{pi} = 1; say %h.perl; #-> { "3.14159265358979" => 1 }
86+
87+
While the invisible quotes around single names is built into C<=>>,
88+
string conversion is not built into the braces: it is a behavior
89+
of the default C<Hash>. Not all types of hashes or collections
90+
do so:
91+
92+
my %h := MixHash.new;
93+
$h{pi} = 1; %h.perl.say; #-> (3.14159265358979e0=>1).MixHash
94+
95+
(Any name that C<=>> would convert to a string can also be used to build
96+
a pair using "adverbial notation" and will appear that way when viewed
97+
through C<.perl>, which is why we see C<:pi(1)> above.)
98+
6599
=end item1
66100
67101
Subscripts can be applied to any expression that returns a subscriptable
@@ -97,8 +131,9 @@ is L<Any>):
97131
However, other types of collections may react differently to subscripts that
98132
address nonexistent elements:
99133
100-
say (0, 10, 20)[3]; #-> Nil
101-
say bag(<a a b b b>)<c>; #-> 0
134+
say (0, 10, 20)[3]; #-> Nil
135+
say bag(<a a b b b>)<c>; #-> 0
136+
say array[uint8].new(1,2)[2] #-> 0
102137
103138
To silently skip nonexistent elements in a subscripting operation, see
104139
L<#Truncating slices> and the L<#:v> adverb.

0 commit comments

Comments
 (0)