EXPORTHOW::DECLARE and role-like declaration

最后都变了- 提交于 2021-02-08 12:52:05

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!