@@ -79,12 +79,37 @@ It is also possible to initialize a single key with the use of C<{}>:
79
79
say $sh; # OUTPUT: «SetHash(key1 key2)»
80
80
say $sh.keys.perl; # OUTPUT: «("key1", "key2").Seq»
81
81
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:
83
84
84
85
my $sh = SetHash.new;
85
86
$sh{ 'a', 'b', 'c' } = True, False, True;
86
87
say $sh.keys.perl; # OUTPUT: «("a", "c").Seq»
87
88
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
+
88
113
= head1 Operators
89
114
90
115
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>
115
140
116
141
= end pod
117
142
118
- # vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
143
+ # vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
0 commit comments