【发布时间】:2017-08-01 02:43:57
【问题描述】:
在没有显式签名的块中区分参数和没有参数的 Perl 6 方法是什么?我对此没有任何实际用途,但我很好奇。
没有显式签名的块将值放入$_:
my &block := { put "The argument was $_" };
签名实际上是;; $_? is raw。这是一个可选参数。 @_ 变量未在块中定义,因为没有显式签名。
没有参数,$_ 将是未定义的:
&block(); # no argument
但还有一种情况,$_ 将是未定义的。类型对象总是未定义的:
&block(Int);
但是,没有任何内容的$_ 实际上是Any(而不是Nil)。我分不清这两种情况的区别:
&block();
&block(Any);
这是一个更长的例子:
my $block := {
say "\t.perl is {$_.perl}";
if $_ ~~ Nil {
put "\tArgument is Nil"
}
elsif ! .defined and $_.^name eq 'Any' {
put "\tArgument is an Any type object"
}
elsif $_ ~~ Any {
put "\tArgument is {$_.^name} type object"
}
else {
put "\tArgument is $_";
}
};
put "No argument: "; $block();
put "Empty argument: "; $block(Empty);
put "Nil argument: "; $block(Nil);
put "Any argument: "; $block(Any);
put "Int argument: "; $block(Int);
注意没有参数和任何参数形式显示相同的东西:
No argument:
.perl is Any
Argument is an Any type object
Empty argument:
.perl is Empty
Argument is Slip type object
Nil argument:
.perl is Nil
Argument is Nil
Any argument:
.perl is Any
Argument is an Any type object
Int argument:
.perl is Int
Argument is Int type object
【问题讨论】:
标签: arguments subroutine raku