包名前的关键字 'unit' 有什么作用?
What does the keyword 'unit' before a package name do?
在下面的代码中;
unit module Fancy::Calculator;
'unit'实际上是做什么的?我知道模块定义的范围成为其声明的文件 - 而不是;
module Fancy::Calculator {
# module definition statements here
}
范围显然是由卷曲定义的,但我在文档中看不到任何明确说明它确实是 all 的内容,我会是如果这就是它所做的一切,那就不足为奇了。其次,在做出这样的声明之后,是否可以在中途声明 unit class Whatever
(class,模块,等等)并结束之前的范围定义?
来自一位评论者(感谢 Brad),看来这就是它所做的一切。至于在同一个文件中启动第二个模块——你不能再次使用单元模块——它会产生;
===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
在下面的代码中;
unit module Fancy::Calculator;
'unit'实际上是做什么的?我知道模块定义的范围成为其声明的文件 - 而不是;
module Fancy::Calculator {
# module definition statements here
}
范围显然是由卷曲定义的,但我在文档中看不到任何明确说明它确实是 all 的内容,我会是如果这就是它所做的一切,那就不足为奇了。其次,在做出这样的声明之后,是否可以在中途声明 unit class Whatever
(class,模块,等等)并结束之前的范围定义?
来自一位评论者(感谢 Brad),看来这就是它所做的一切。至于在同一个文件中启动第二个模块——你不能再次使用单元模块——它会产生;
===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