Skip to content

Commit 5465e22

Browse files
committed
More clarifications on hash definition
1 parent 155cbd0 commit 5465e22

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

doc/Language/hashmap.pod6

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ binding:
156156
my %h := { a => 'b', c => 'd', e => 'f'};
157157
say %h; # OUTPUT: «{a => b, c => d, e => f}␤»
158158
159+
Nested hashes can also be defined using the same syntax:
160+
161+
my %h = e => f => 'g';
162+
say %h<e><f>; # OUTPUT: «g␤»
163+
164+
However, what you are defining here is a key pointing to a L<Pair>, which is
165+
fine if that is what you want, and if your nested hash has got a single key. But
166+
C<%h<e>> will point to a C<Pair> which will have these consequences:
167+
168+
=begin code :skip-test<Illustrates an error>
169+
my %h = e => f => 'g';
170+
%h<e><q> = 'k';
171+
# OUTPUT: «(exit code 1) Pair␤Cannot modify an immutable Str (Nil)␤ in block <unit>»
172+
173+
This, however, will effectively define a nested hash:
174+
175+
m: my %h = e => { f => 'g'};
176+
say %h<e>.^name; # OUTPUT: «Hash␤»
177+
say %h<e><f>; # OUTPUT: «g␤»
159178
160179
If a L<Pair> is encountered where a value is expected, it is used as a
161180
hash value:

0 commit comments

Comments
 (0)