假设我经常使用一组样板:
class Foo {
method abc($a: $b, $c, +@d) is pure {
use Slang::Bar;
…
}
method xyz($a: $b, $c, +@d) is pure {
use Slang::Bar;
…
}
method blarg($a: $b, $c, +@d) is pure {
use Slang::Bar;
…
}
}
Run Code Online (Sandbox Code Playgroud)
我宁愿只能说:
class Foo is/does Bar {
bar abc { … }
bar xyz { … }
bar blarg { … }
}
Run Code Online (Sandbox Code Playgroud)
在 Bar 中的某处,为 bar 设置声明(或者,由于类 Foo 最终将使用自己的声明符,因此它可以放在其他地方并且不必在单独的类型中拉出)。我该怎么做?
方法EXPORTHOW在当前调用.set_how$?LANG向后者添加俚语。
然后,它add_package_declarator给MAIN $?LANG它增加了一个package_declarator方法,它的操作和语法。我认为这是唯一的“动态俚语”(在 World.nqp 中)。
如果你想要的是覆盖routine_declarator。然后你必须写一个俚语来模仿刚刚引用的链。如果你接受保留method关键字并在类中进行自动签名,根据方法名说,这里有一个方法:
注意: 包是一个容器(包、语法、模块、角色、专有技术、枚举、类、子集)。如果您将代码像方法一样放入其中,则会执行此操作(我刚刚尝试过):
我会在模块中使用未记录的EXPORTHOW,因为我没有找到Phaser的方法。显然,即使在 BEGIN 也为时已晚。DECLARE
我给出的例子是装饰类中的每个方法(甚至BUILDALL)。
decorator.rakumod)class DecoratedClassHOW is Metamodel::ClassHOW {
method add_method(Mu $obj, $name, $code_obj) {
sub wrapper ($obj, $a, $b) {
say "Before $name";
my $res = $code_obj($obj, $a, $b);
say "After $name";
return $res;
}
my $res = callwith($obj, $name, &wrapper);
return $res;
}
}
my module EXPORTHOW {
package DECLARE {
constant decorated = DecoratedClassHOW;
}
}
Run Code Online (Sandbox Code Playgroud)
use lib '.';
use decorator-lib;
decorated Foo {
method abc($a, $b) {
say "In abc: $a:$b";
}
}
my $f = Foo.new;
$f.abc(1, 2);
Run Code Online (Sandbox Code Playgroud)
Before BUILDALL
After BUILDALL
Before abc
In abc: 1:2
After abc
Run Code Online (Sandbox Code Playgroud)
for $_.HOW.method_table($_)创建一个新的散列,用 (well-named) 覆盖方法缓存nqp::setmethcache。| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |