在 Raku 中,不能从同一文件中定义的类继承方法特征



用trait_mod定义的方法特征完美地继承自其他文件中定义的类。 当在同一文件中定义两个类时,这似乎不起作用。

以下 2 个文件协同工作正常:

# mmm.pm6
class TTT is export  {
multi trait_mod:<is> (Routine $meth, :$something! ) is export
{
say "METH : ", $meth.name;
}
}
# aaa.p6
use lib <.>;
use mmm;
class BBB is TTT {
method work is something {  }
}

输出为:METH : work

但是在以下唯一文件中收集的相同代码会给出错误消息

# bbb.p6
class TTT is export  {
multi trait_mod:<is> (Routine $meth, :$something! ) is export
{
say "METH : ", $meth.name;
}
}
class BBB is TTT {
method work is something {  }
}

输出为:

===SORRY!=== Error while compiling /home/home.mg6/yves/ygsrc/trait/bbb.p6
Can't use unknown trait 'is' -> 'something' in a method declaration.
at /home/home.mg6/yves/ygsrc/trait/bbb.p6:12
expecting any of:
rw raw hidden-from-backtrace hidden-from-USAGE pure default
DEPRECATED inlinable nodal prec equiv tighter looser assoc
leading_docs trailing_docs

我正在运行这个乐道版本:

This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.

为什么我不能从单个文件运行此代码? 我错过了什么?

根据文档:

模块内容(类、子例程、变量等(可以是 从具有is export特征的模块导出;这些是可用的 在调用方的命名空间中导入模块后importuse.

您可以尝试使用importis特征导入到当前包中:

bbb.p6

module X {  # <- declare a module name, e.g. 'X' so we can import it later..
class TTT is export  {
multi trait_mod:<is> (Routine $meth, :$something! ) is export
{
say "METH : ", $meth.name;
}
}
}
import X;
class BBB is TTT {
method work is something {  }
}

最新更新