Skip to content

Commit b97f1c7

Browse files
committed
Document Metamodel::Primitives' parameterization cache API
This is *very* handy in MOP-heavy code, but is rather unintuitive. Adds "parameterizer" and "noop" as words.
1 parent 950e714 commit b97f1c7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

doc/Type/Metamodel/Primitives.pod6

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ of just one method called C<why>:
2424
CATCH { default { put .^name, ': ', .Str } };
2525
# OUTPUT: «X::Method::NotFound: Method 'list' not found for invocant of class 'why oh why?'␤»
2626
27+
Every metaobject has the capacity to hold a parameterization cache. This is
28+
distinct from the C<parameterize> metamethod backing parameterization syntax.
29+
For example, a package can be parametric in this low-level sense:
30+
31+
package Cache {
32+
our sub parameterize(+args) is raw {
33+
Metamodel::Primitives.parameterize_type: $?PACKAGE, args
34+
}
35+
36+
sub noop(Mu, Mu \args) is raw {
37+
args
38+
}
39+
40+
BEGIN Metamodel::Primitives.set_parameterizer: $?PACKAGE, &noop;
41+
}
42+
2743
=head1 Methods
2844
2945
=head2 method create_type
@@ -93,4 +109,44 @@ L<special arrangements|https://github.com/rakudo/rakudo/issues/3414#issuecomment
93109
94110
Type-checks C<obj> against C<type>
95111
112+
=head2 method set_parameterizer
113+
114+
method set_parameterizer(Mu \obj, &parameterizer --> Nil)
115+
116+
Initializes the parameterization cache for a metaobject. This incorporates the
117+
C<&parameterize> routine to produce parameterizations to be cached. This is
118+
assumed to carry a signature compatible with C<:(Mu $root, List:D $args)>,
119+
C<$root> being the base metaobject for the parameterization, C<$args> being the
120+
type arguments' object buffer.
121+
122+
=head2 method parameterize_type
123+
124+
method parameterize_type(Mu \obj, +parameters --> Mu)
125+
126+
Parameterizes a metaobject prepared by C<set_parameterizer> with C<parameters>.
127+
The resulting metaobject is cached by literal object comparisons with C<=:=>
128+
for each element of C<parameters>. I<Containers tend to invalidate any match>.
129+
130+
=head2 method type_parameterized
131+
132+
method type_parameterized(Mu \obj --> Mu)
133+
134+
Returns the base metaobject from a parameterization given its resulting C<obj>.
135+
Returns C<Mu> if none was ever performed.
136+
137+
=head2 method type_parameters
138+
139+
method type_parameters(Mu \obj --> List:D)
140+
141+
Returns the type arguments' object buffer from a parameterization given its
142+
resulting C<obj>. Dies if none was ever performed.
143+
144+
=head2 method type_parameter_at
145+
146+
method type_parameter_at(Mu \obj, Int:D \idx --> Mu) is raw
147+
148+
Returns a particular object from a parameterization given its resulting C<obj>
149+
and an index, skipping the C<List>-building step of C<type_parameters>. Dies if
150+
none was ever performed.
151+
96152
=end pod

xt/pws/words.pws

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ nominalized
906906
nonchaining
907907
nonintuitive
908908
nonspacing
909+
noop
909910
nopackage
910911
noproto
911912
noself
@@ -994,6 +995,7 @@ parallelize
994995
param
995996
parameterization
996997
parameterizations
998+
parameterizer
997999
parameterizes
9981000
parameterizing
9991001
parameterless

0 commit comments

Comments
 (0)