The ^parameterize
method allows one to parameterize a class with some given information, such as a role.
my role A[::T = Mu] { }
my class B {
method ^parameterize(Mu \base, Mu \type) {
my \what := base.^mixin(A[type]);
what.^set_name(base.^name ~ '[' ~ type.^name ~ ']');
what
}
}
dd B[Int]; # B[Int]
dd B[Int].^roles; # (A[Int],)
However, I haven't been able to find a way to make the unparameterized version of B
do the unparameterized version of role A
.
dd B; # B
dd B.^roles; # (), want to see (A[Mu],)
I tried to add a ^compose
method, but that only gets called if the class is parameterized.
Suggestions very welcome!
I've come to the conclusion that you can't (at least not in the foreseeable future). So I took a step back to see what I actually wanted to achieve. And that turned out to be possible:
my role A[::T] {
method foobar() { T }
}
my class B {
method foobar() { Mu }
method ^parameterize(Mu \base, Mu \type) {
my \what := base.^mixin(A[type]);
what.^set_name(base.^name ~ '[' ~ type.^name ~ ']');
what
}
}
dd B.foobar; # Mu
dd B[Int].foobar; # Int
The method foobar
is by default supplied by the B
class. If one parameterizes the B
class, it will mix in another version of the foobar
method (which shadows the original method), which returns the type given with the parameterization.
In the end, this gave me the framework for allowing parameterization of QuantHash
es, a long standing request of my own, and recently others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With