用 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
我正在运行这个 Rakudo 版本:
This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.
为什么我不能从单个文件运行此代码?
我错过了什么 ?
最佳答案
根据documentation :
Module contents (classes, subroutines, variables, etc.) can be exported from a module with the
is export
trait; these are available in the caller's namespace once the module has been imported withimport
oruse
.
您可以尝试使用
import
导入 is
trait 到当前包中: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 { }
}
关于raku - 在 Raku 中,无法从同一文件中定义的类继承方法特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59147644/