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

标签 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

我正在运行这个 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 with import or use.



您可以尝试使用 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/

相关文章:

haskell - 在 raku 的模块中使用 Haskell 之类的 Prelude 模块

raku - 返回多个数组时避免创建临时标量

csv - 如何定义 Raku 语法来解析 TSV 文本?

raku - 在 perl6 中运行命令,在 shell 中运行的命令在 perl6 中运行时会产生失败

raku - Perl 6 如何评估真实性?

string - 如何在 Perl 6 中打开一个字符串的文件句柄?

regex - 如何通过代码点而不是字素替换 Perl 6 中的字符串?

oop - 在 Perl 6 中继承私有(private)属性

exception - Perl 5 的鲤鱼的 raku 类似物是什么?

python - 我知道 Perl 5。学习 Perl 6 而不是转向 Python 有什么好处?