Skip to content

Commit 8a76fff

Browse files
committed
Elaborate a bit about my %s is SetHash
Also noting the new parameterization feature as available since 2019.01
1 parent 2dda092 commit 8a76fff

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

doc/Type/SetHash.pod6

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,37 @@ It is also possible to initialize a single key with the use of C<{}>:
7979
say $sh; # OUTPUT: «SetHash(key1 key2)␤»
8080
say $sh.keys.perl; # OUTPUT: «("key1", "key2").Seq␤»
8181
82-
or, in order to initialize more than one key at the same time, use a list assignment:
82+
or, in order to initialize more than one key at the same time, use a list
83+
assignment:
8384
8485
my $sh = SetHash.new;
8586
$sh{ 'a', 'b', 'c' } = True, False, True;
8687
say $sh.keys.perl; # OUTPUT: «("a", "c").Seq␤»
8788
89+
You can also create C<SetHash> masquerading as a hash by using the C<is> trait:
90+
91+
my %sh is SetHash = <a b c>;
92+
say %sh<a>; # True
93+
say %sh<d>; # False
94+
95+
Since 6.d (2019.01 and later) it is also possible to specify the type of values
96+
you would like to allow in a C<SetHash>. This can either be done when calling
97+
C<.new>:
98+
99+
# only allow Pairs
100+
my $n = SetHash[Pair].new: "zero" => 0, "one" => 1, "two" => 2;
101+
102+
or using the masquerading syntax:
103+
104+
# only allow strings
105+
my %sh is SetHash[Str] = <a b c>;
106+
say %sh<a>; # True
107+
say %sh<d>; # False
108+
109+
# only allow whole numbers
110+
my %sh is SetHash[Int] = <a b c>;
111+
# Type check failed in binding; expected Int but got Str ("a")
112+
88113
=head1 Operators
89114
90115
Perl 6 provides common set operators, which can take C<SetHash>es (or any other
@@ -115,4 +140,4 @@ L<Sets, Bags, and Mixes|/language/setbagmix>
115140
116141
=end pod
117142

118-
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
143+
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)