doc
doc copied to clipboard
document parametric classes
The problem
I want to define a type-parametric class. I can't find how to do that in the documentation.
Suggestions
Some example, at least, like (a correct version of):
role Foo[::X] { };
class Bar {
method ^parameterize(Any $, Any:U $param) {
return Bar but Foo[$param]
}
}
say Bar[Str].perl;
say Bar[Str] ~~ Foo;
say Bar[Str] ~~ Foo[Str];
but explaining what the first argument to ClassHOW.parameterize
is (it's a string with the class name, in Rakudo 2019.07.1)
Noted. Thanks!
(it's a string with the class name, in Rakudo 2019.07.1)
It should be the metaobject the metamethod corresponds to:
class Foo {
role Bar[Mu \T] { }
method ^parameterize(Foo:U $this is raw, Mu \T --> Mu) {
my Mu $mixin := $this.^mixin: Bar.^parameterize: T;
$mixin.^set_name: $this.^name ~ '[' ~ T.^name ~ ']';
$mixin
}
};
say Foo[Int] ~~ Foo; # OUTPUT: True
say Foo[Int] ~~ Foo::Bar[Int]; # OUTPUT: True
This uses .^mixin
and .^parameterize
directly instead of through but
and []
to prevent the mixin of the parameterization from happening before the method actually gets called (which messes with generics if they're getting involved), and resets its name similarly to how parameterized roles set theirs. I think Metamodel::Mixins
should be documented beforehand so people can understand everything this is doing though, and there should be other examples of how parameterization can be used so people don't get the impression that mixins are all parameterization is useful for.