Skip to content

Commit

Permalink
Clarifications and some examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Jan 5, 2021
1 parent 373de7c commit 4f10478
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions doc/Type/Associative.pod6
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
role Associative[::TValue = Mu, ::TKey = Str(Any)] { }
A common role for types that support name-based lookup through
L«postcircumfix:<{ }>|/language/operators#postcircumfix_{_}», for example
L«postcircumfix:<{ }>|/language/operators#postcircumfix_{_}» such as
L<Hash|/type/Hash> and L<Map|/type/Map>. It is used for type checks in operators
that expect to find specific methods to call. See
L<Subscripts|/language/subscripts#Methods_to_implement_for_associative_subscripting>
Expand All @@ -23,7 +23,8 @@ my %whatever := Whatever.new;
=end code
Please note that we are using binding C<:=> here, since by default C<%>
assignments expect a C<Hash> in the right-hand side. However, with the
assignments expect a C<Hash> in the right-hand side, and thus assignment
would try and convert it to a hash (also failing). However, with the
Associative role:
class Whatever is Associative {};
Expand All @@ -39,11 +40,11 @@ Defined as:
method of()
C<Associative> is actually a
C<Associative>, as the definition above shows, is actually a
L<parameterized role|/language/objects#Parameterized_roles>
which can use different classes for keys and values. As seen at the top of the
document, by default it coerces to C<Str> for the key and uses a very generic
C<Mu> for value.
document, by default it coerces the key to C<Str> and uses a very
generic C<Mu> for value.
my %any-hash;
say %any-hash.of; # OUTPUT: «(Mu)␤»
Expand All @@ -68,17 +69,24 @@ you use the parameterized version of Associative.
my %any-hash;
%any-hash.keyof; # OUTPUT: «(Str(Any))␤»
=head1 Methods that should be provided
=head1 Methods that classes mixing Associative should provide
You need to provide these methods if you want your class to implement the
Associative role.
Associative role properly and, thus, use the C<{}> operator for accessing the
value given a key. They are not mandatory, however; on the other hand, if you
simply want objects of a class to use C<{}>, you can implement them without
mixing the C<Associative> role.
=head2 method AT-KEY
method AT-KEY(\key)
Should return the value / container at the given key.
=for code
class What { method AT-KEY(\key) { 42 }};
say What.new{33}; # OUTPUT: «42␤»
=head2 method EXISTS-KEY
method EXISTS-KEY(\key)
Expand All @@ -103,7 +111,8 @@ the object for the first time. Should return the invocant.
=head1 See also
See L<Methods to implement for associative subscripting|/language/subscripts#Methods_to_implement_for_associative_subscripting>
See
L<Methods to implement for associative subscripting|/language/subscripts#Methods_to_implement_for_associative_subscripting>
for information about additional methods that can be implemented for the
C<Associative> role.
Expand Down

0 comments on commit 4f10478

Please sign in to comment.