Skip to content

Commit 50d18c2

Browse files
committed
Modified examples with hashes to use %() hash literal syntax
Modified section in syntax file to replace instances of the { } operator with the preferred hash literal constructor %(). Documentation of the { } is still in doc/Type/Hash.pod6. Modified existing examples to use the %() hash literal as mentioned in Issue #1380.
1 parent 2ca0128 commit 50d18c2

File tree

9 files changed

+33
-32
lines changed

9 files changed

+33
-32
lines changed

doc/Language/5to6-nutshell.pod6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,8 @@ right-hand side of the assignment will be interpreted as a L<Block|/type/Block>,
452452
not a Hash:
453453
454454
my @people = [
455-
{ id => "1A", firstName => "Andy", lastName => "Adams" },
456-
{ id => "2B", firstName => "Beth", lastName => "Burke" },
455+
%( id => "1A", firstName => "Andy", lastName => "Adams" ),
456+
%( id => "2B", firstName => "Beth", lastName => "Burke" ),
457457
# ...
458458
];
459459

doc/Language/faq.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ for dumping, whose output is similar to L<perl>, but with more information.
215215
216216
Examples:
217217
218-
my $foo = { foo => 'bar' };
218+
my $foo = %( foo => 'bar' );
219219
say $foo.perl; # OUTPUT: «${:foo("bar")}␤»
220220
say $foo; # OUTPUT: «{foo => bar}␤»
221221

doc/Language/modules.pod6

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,9 @@ passing C<:DEFAULT> to C<use> along with your positional parameters.
335335
class MyModule::Class {}
336336
337337
sub EXPORT($short_name?) {
338-
{
338+
%(
339339
do $short_name => MyModule::Class if $short_name
340-
}
340+
)
341341
}
342342
343343
sub always is export(:MANDATORY) { say "works" }
@@ -364,9 +364,9 @@ L<Cool>s.
364364
# lib/MakeQuestionable.pm
365365
sub EXPORT(::Questionable) {
366366
my multi postfix:<?>(Questionable $_) { .so };
367-
{
367+
%(
368368
'&postfix:<?>' => &postfix:<?>,
369-
}
369+
)
370370
}
371371
=end code
372372

doc/Language/syntax.pod6

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -525,23 +525,24 @@ Use the L<Slip> prefix operator (C<|>) to flatten the needed items:
525525
526526
=head3 Hash literals
527527
528-
A pair of curly braces can surround a list of pairs to form a
529-
L<Hash|/type/Hash> literal; typically there is a comma-delimited list of pairs
530-
inside. If a non-pair is used, it is assumed to be a key and the next element
531-
is the value. Most often this is used with simple arrow pairs.
528+
A leading associative sigil and pair of parenthesis C<%( )> can surround a C<List> of
529+
C<Pairs> to form a L<Hash|/type/Hash> literal; typically there is a comma-delimited
530+
C<List> of C<Pairs> inside. If a non-pair is used, it is assumed to be a key and
531+
the next element is the value. Most often this is used with simple arrow pairs.
532532
533-
say { a => 3, b => 23, :foo, :dog<cat>, "french", "fries" };
533+
say %( a => 3, b => 23, :foo, :dog<cat>, "french", "fries" );
534534
# OUTPUT: «a => 3, b => 23, dog => cat, foo => True, french => fries␤»
535535
536-
say {a => 73, foo => "fish"}.keys.join(" "); # OUTPUT: «a foo␤»
537-
# ^^^^^^^^^^^^^^^^^^^^^^^^ Hash constructor
536+
say %(a => 73, foo => "fish").keys.join(" "); # OUTPUT: «a foo␤»
537+
# ^^^^^^^^^^^^^^^^^^^^^^^^^ Hash constructor
538538
539-
When assigning to a C<%> sigil variable, the curly braces are optional.
539+
When assigning to a C<%> sigiled variable on the LHS, the sigil and parenthesis
540+
surrounding the RHS C<Pairs> are optional.
540541
541542
my %ages = fred => 23, jean => 87, ann => 4;
542543
543-
By default keys in C<{ }> are forced to strings. To compose a hash with
544-
non-string keys, use a colon prefix:
544+
By default, keys in C<%( )> are forced to strings. To compose a hash with
545+
non-string keys, use curly brace delimiters with a colon prefix C<:{ }> :
545546
546547
my $when = :{ (now) => "Instant", (DateTime.now) => "DateTime" };
547548

doc/Type/Any.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Defined as:
309309
Forces given object to be evaluated in item context and returns the value of it.
310310
311311
say item([1,2,3]).perl; # OUTPUT: «$[1, 2, 3]␤»
312-
say item({ apple => 10 }).perl; # OUTPUT: «${:apple(10)}␤»
312+
say item( %( apple => 10 ) ).perl; # OUTPUT: «${:apple(10)}␤»
313313
say item("abc").perl; # OUTPUT: «"abc"␤»
314314
315315
You can also use C<$> as item contextualizer.

doc/Type/Hash.pod6

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,22 @@ not a Hash:
7878
7979
=begin code :skip-test
8080
my @people = [
81-
{ id => "1A", firstName => "Andy", lastName => "Adams" },
82-
{ id => "2B", firstName => "Beth", lastName => "Burke" },
81+
%( id => "1A", firstName => "Andy", lastName => "Adams" ),
82+
%( id => "2B", firstName => "Beth", lastName => "Burke" ),
8383
# ...
8484
];
8585
8686
sub lookup-user (Hash $h) { #`(Do something...) $h }
8787
8888
my @names = map {
8989
# While this creates a hash:
90-
my $query = { name => "$person<firstName> $person<lastName>" };
90+
my $query = { name => "$person<firstName> $person<lastName>" };
9191
say $query.^name; # OUTPUT: «Hash␤»
92-
# And this also makes a hash
92+
9393
# Doing this will create a Block. Oh no!
94-
my $query = { name => "$_<firstName> $_<lastName>" };
94+
my $query2 = { name => "$_<firstName> $_<lastName>" };
9595
say $query2.^name; # OUTPUT: «Block␤»
96-
say $query2<name>; # fails
96+
say $query2<name>; # fails
9797
9898
CATCH { default { put .^name, ': ', .Str } };
9999
# OUTPUT: «X::AdHoc: Type Block does not support associative indexing.␤»
@@ -540,7 +540,7 @@ arguments|/type/Signature#Positional_vs._Named> to C<.append>.
540540
541541
my %h = a => 1;
542542
%h.append('b', 2, 'c', 3);
543-
%h.append({d => 4});
543+
%h.append( %(d => 4) );
544544
say %h;
545545
# OUTPUT: «{a => 1, b => 2, c => 3, d => 4}␤»
546546
%h.append('a', 2);

doc/Type/List.pod6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ comparator.
764764
765765
Example:
766766
767-
my @list = {a => 42}, {b => 13}, {a => 42};
767+
my @list = %(a => 42), %(b => 13), %(a => 42);
768768
say @list.unique(:with(&[eqv])) # OUTPUT: «({a => 42} {b => 13})␤»
769769
770770
B<Note:> since C<:with> L<Callable> has to be tried with all the items in the list,
@@ -789,7 +789,7 @@ Examples:
789789
say <a b b c c b a>.repeated; # OUTPUT: «(b c b a)␤»
790790
say <a A B b c b C>.repeated(:as(&lc)); # OUTPUT: «(A b b C)␤»
791791
792-
my @list = {a => 42}, {b => 13}, {a => 42};
792+
my @list = %(a => 42), %(b => 13), %(a => 42);
793793
say @list.repeated(:with(&[eqv])) # OUTPUT: «({a => 42})␤»
794794
795795
=head2 routine squish

doc/Type/Mixy.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ C<Mixy> are L<Real>s rather than L<Int>s.
1919
Returns the sum of all the weights
2020
2121
mix('a', 'b', 'c', 'a', 'a', 'd').total == 6; # RESULT: «True»
22-
{a => 5.6, b => 2.4}.Mix.total == 8; # RESULT: «True»
22+
%(a => 5.6, b => 2.4).Mix.total == 8; # RESULT: «True»
2323
2424
=head2 method roll
2525

doc/Type/Mu.pod6

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ empty L<string|/type/Str> or numerical zeros
8080
say Mu.new.Bool; # OUTPUT: «True␤»
8181
say [1, 2, 3].Bool; # OUTPUT: «True␤»
8282
say [].Bool; # OUTPUT: «False␤»
83-
say { 'hash' => 'full' }.Bool; # OUTPUT: «True␤»
83+
say %( hash => 'full' ).Bool; # OUTPUT: «True␤»
8484
say {}.Bool; # OUTPUT: «False␤»
8585
say "".Bool; # OUTPUT: «False␤»
8686
say 0.Bool; # OUTPUT: «False␤»
@@ -147,9 +147,9 @@ write a Perl expression that produces a particular value
147147
148148
Forces the invocant to be evaluated in item context and returns the value of it.
149149
150-
say [1,2,3].item.perl; # OUTPUT: «$[1, 2, 3]␤»
151-
say { apple => 10 }.item.perl; # OUTPUT: «${:apple(10)}␤»
152-
say "abc".item.perl; # OUTPUT: «"abc"␤»
150+
say [1,2,3].item.perl; # OUTPUT: «$[1, 2, 3]␤»
151+
say %( apple => 10 ).item.perl; # OUTPUT: «${:apple(10)}␤»
152+
say "abc".item.perl; # OUTPUT: «"abc"␤»
153153
154154
=head2 method self
155155

0 commit comments

Comments
 (0)