EXPORTHOW::DECLARE 和类似角色的声明 [英] EXPORTHOW::DECLARE and role-like declaration
问题描述
我需要一个角色,通过从Metamodel::ParametricRoleHOW
继承的Metamodel
类实现一些特殊功能.应用它的一种方法是使用特征.但我想要一个很好的语法糖,就像我们可以用类做的那样:
特殊矿井{...}
好的,我将以下内容添加到我的模块中:
我的包 EXPORTHOW {包声明{常量特殊 = My::Metamodel::SpecialRoleHOW;}}
一切都运行顺利......不,它不是:
class Foo 是我的 {...}
我最终得到:
<块引用>无法实例化角色我的":
对Actions.nqp
的一个非常简短的检查发现角色有特殊处理,但仅当role
关键字用于包声明时.因此,我得出结论,获得语法糖的唯一方法是将我自己的规则混合到主语法中并模拟角色声明.幸运的是,package_def
依赖于 $*PKGDECL
,它使这种模拟成为可能.
我的结论是否正确?还是有更简单的方法来实现目标?
请求的功能尚不可用.但是 AFAIK,Jonathan Worthington 正在研究从元类派生包类型的代码.不知道他在这方面有多远.
同时,可以通过 Perl 6 语法的运行时扩展来实现所需的功能.我已经为 OO::Plugin 模块完成了.>
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?
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.
这篇关于EXPORTHOW::DECLARE 和类似角色的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!