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

ygu*_*mot 7 raku

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

以下 2 个文件可以很好地协同工作:

# mmm.pm6

class TTT is export  {

    multi trait_mod:<is> (Routine $meth, :$something! ) is export
    {
        say "METH : ", $meth.name;
    }
}
Run Code Online (Sandbox Code Playgroud)
# aaa.p6

use lib <.>;
use mmm;

class BBB is TTT {
    method work is something {  }
}
Run Code Online (Sandbox Code Playgroud)

输出是: 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 {  }
}
Run Code Online (Sandbox Code Playgroud)

输出是:

===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
Run Code Online (Sandbox Code Playgroud)

我正在运行这个 Rakudo 版本:

This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
implementing Perl 6.d.
Run Code Online (Sandbox Code Playgroud)

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

Håk*_*and 8

根据文档

模块内容(类、子程序、变量等)可以从具有is exporttrait的模块导出;使用import或导入模块后,这些在调用者的命名空间中可用 use

您可以尝试使用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 {  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我发现“导入 TTT;” 也可以在不使用模块声明的情况下使用。 (4认同)