包名前的关键字“unit”是做什么的?

时间:2016-02-28 02:28:42

标签: perl6

在以下代码中;

Panel panel = (Panel)label.Parent;

'单位'实际上做了什么?我知道模块定义的范围变成了它声明的文件 - 而不是;

unit module Fancy::Calculator;

其中范围显然是由curlies定义的,但我在文档中看不到任何明确表明它是全部的内容,如果这是全部的话我会有点惊讶它做到了。其次,在做出这样的声明之后,可以将module Fancy::Calculator { # module definition statements here } (类,模块,等等)声明为一半,并调用结束前一个作用域定义吗?

1 个答案:

答案 0 :(得分:2)

来自评论者(感谢布拉德),似乎就是这样。至于在同一个文件中启动第二个模块 - 你不能再使用单元模块 - 这会产生;

===SORRY!=== Error while compiling /home/user/Fancy/Calculator.pm6
Too late for unit-scoped module definition;
Please use the block form.

...但是正如消息所说,您可以使用块形式,但无论您声明的是在单元模块命名空间内 - 在这种情况下是Fancy :: Calculator。所以这些;

unit module Fancy::Calculator;

# The following available to the module user as Fancy::Calculator::Adder
class Adder {
    method add { "Hi... I am a method who can add" }
}

# Starting definition of new module but its within Fancy::Calculator namespace
module Minus {

    # Following available to the module user as Fancy::Calculator::Minus::Subber
    class Subber {
        method subtract { "Hi... I am a method who can subtract" }
    }

    # unless you add "is export" in which case its available by its short name
    class Multiplyer is export {
        method times { "Hi... I am a method who can multiply" }
    }

    sub divide() is export { "Hi... I am a sub who can divide" }
}
像这样访问

;

# In main
use Fancy::Calculator;

my $fca = Fancy::Calculator::Adder.new;
say $fca.add;              # Hi... I am a method who can add
my $fcms = Fancy::Calculator::Minus::Subber.new;
say $fcms.subtract;        # Hi... I am a method who can subtract
my $mul = Multiplyer.new;
say $mul.times;            # Hi... I am a sub who can multiply
say divide();              # Hi... I am a sub who can divide