Skip to content

Commit 5583146

Browse files
committed
Updates Associative, closes #2062
1 parent 38b9e76 commit 5583146

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

doc/Type/Associative.pod6

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,52 @@
44
55
=SUBTITLE Object that supports looking up values by key
66
7-
role Associative { }
7+
role Associative[::TValue = Mu, ::TKey = Str(Any)] { }
88
99
A common role for types that support name-based lookup through
10-
L«postcircumfix:<{ }>|/language/operators#postcircumfix_{_}», for example L<Hash> and L<Map>.
11-
It is used for type checks in operators that expect to find specific methods to
12-
call. See L<Subscripts|/language/subscripts#Methods_to_implement_for_associative_subscripting> for details.
10+
L«postcircumfix:<{ }>|/language/operators#postcircumfix_{_}», for example
11+
L<Hash> and L<Map>. It is used for type checks in operators that expect to find
12+
specific methods to call. See
13+
L<Subscripts|/language/subscripts#Methods_to_implement_for_associative_subscripting>
14+
for details.
1315
1416
The C<%> sigil restricts variables to objects that do C<Associative>.
1517
16-
Associative does not provide any methods.
18+
=head1 Methods
19+
20+
=head2 of
21+
22+
Defined as:
23+
24+
method of()
25+
26+
C<Associative> is actually a L<parametrized role|language/objects#Parameterized_Roles>
27+
which can use different classes for keys and values. As seen above, by default
28+
it coerces to C<Str> for the key and uses a very generic C<Mu> for value.
29+
30+
my %any-hash;
31+
say %any-hash.of;# OUTPUT: «(Mu)␤»
32+
33+
The value is the first parameter you use when instantiating C<Associative> with
34+
particular classes:
35+
36+
class DateHash is Hash does Associative[Cool,DateTime] {};
37+
my %date-hash := DateHash.new;
38+
say %date-hash.of; # OUTPUT: «(Cool)␤»
39+
40+
=head2 keyof
41+
42+
Defined as:
43+
44+
method keyof()
45+
46+
Returns the parametrized key used for the Associative role, which is C<Any>
47+
coerced to C<Str> by default. This is the class used as second parameter when
48+
you use the parametrized version of Associative.
49+
50+
my %any-hash;
51+
%any-hash.keyof; #OUTPUT: «(Str(Any))␤»
52+
1753
1854
=end pod
1955

xt/words.pws

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,4 +1415,6 @@ netbsd
14151415
raspbian
14161416
fabc
14171417
AARGFILES
1418-
Mattijsen
1418+
Mattijsen
1419+
TKey
1420+
DateHash

0 commit comments

Comments
 (0)