问题
I need to have a role with a little bit of special functionality implemented through a Metamodel
class of mine inheriting from Metamodel::ParametricRoleHOW
. One way to apply it is by using a trait. But I want a nice syntax sugar like we can do with classes:
special Mine {
...
}
Ok, I add the following into my module:
my package EXPORTHOW {
package DECLARE {
constant special = My::Metamodel::SpecialRoleHOW;
}
}
and everything runs smoothly... No, it is not:
class Foo does Mine {
...
}
and I end up with:
Could not instantiate role 'Mine':
A very brief inspection of Actions.nqp
revealed that roles have special handling, but only and only when role
keyword is used for package declaration. Hence, I conclude that the only way to have the syntax sugar – is to mixin my own rule into the main grammar and simulate the role declaration. Luckily, package_def
relies upon $*PKGDECL
and it makes such simulation possible.
Am I right in the conclusion? Or a simpler way exists to achieve the goal?
回答1:
The requested functionality is not yet possible. But AFAIK, Jonathan Worthington is working on code which would derive a package type from its metaclass. Not sure as to how far is he on this though.
Meanwhile, the desired functionality could be achieved through run time extending of Perl 6 grammar. I have it done for OO::Plugin module.
来源:https://stackoverflow.com/questions/53907123/exporthowdeclare-and-role-like-declaration