|
4 | 4 |
|
5 | 5 | =SUBTITLE Object that supports looking up values by key |
6 | 6 |
|
7 | | - role Associative { } |
| 7 | + role Associative[::TValue = Mu, ::TKey = Str(Any)] { } |
8 | 8 |
|
9 | 9 | 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. |
13 | 15 |
|
14 | 16 | The C<%> sigil restricts variables to objects that do C<Associative>. |
15 | 17 |
|
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 | +
|
17 | 53 |
|
18 | 54 | =end pod |
19 | 55 |
|
|
0 commit comments