Skip to content

Commit 136acc7

Browse files
committed
Document Foo => 42 trap
Resolves #1721
1 parent 5ac5245 commit 136acc7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/Language/traps.pod6

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,32 @@ If you are working with regexes, then there is no need to use C<.fc>
533533
and you can use C<:i> (C<:ignorecase>) adverb instead.
534534
535535
536+
=head1 Pairs
537+
538+
=head2 Constants on LHS of pair notation
539+
540+
Consider this code:
541+
542+
=begin code
543+
enum Animals <Dog Cat>;
544+
my %h := :{ Dog => 42 };
545+
say %h{Dog}; # OUTPUT: «(Any)␤»
546+
=end code
547+
548+
The C<:{ … }> syntax is used to create
549+
L<object hashes|/type/Hash#Non-string_keys_(object_hash)>. The
550+
intentions of someone who wrote that code were to create a hash with
551+
Enum objects as keys (and C<say %h{Dog}> attempts to get a value using
552+
the Enum object to perform the lookup). However, that's not how pair
553+
notation works.
554+
555+
For exapmple, in C«Dog => 42» the key will be a C<Str>. That is, it
556+
doesn't matter if there is a constant, or an enumeration with the
557+
same name. The pair notation will always use the left-hand side as a
558+
string literal, as long as it looks like an identifier.
559+
560+
To avoid this, use C«(Dog) => 42» or C«::Dog => 42».
561+
536562
=head1 Operators
537563
538564
Some operators commonly shared among other languages were repurposed in Perl 6 for other, more common, things:

0 commit comments

Comments
 (0)