Syntax 什么是-->;在Perl 6中是什么意思?
在Rossetta代码上,子例程签名包含Syntax 什么是-->;在Perl 6中是什么意思?,syntax,raku,Syntax,Raku,在Rossetta代码上,子例程签名包含Str$t-->Int -->是一个作用于$t的操作符还是其他的东西?它指定了一个 例如,此代码要求返回值为整数: sub add (Int $inputA, Int $inputB --> Int) { my $result = $inputA+$inputB; say $result; # Oops, this is the last statement, so its return value is used
Str$t-->Int
-->
是一个作用于$t
的操作符还是其他的东西?它指定了一个
例如,此代码要求返回值为整数:
sub add (Int $inputA, Int $inputB --> Int)
{
my $result = $inputA+$inputB;
say $result; # Oops, this is the last statement, so its return value is used for the subroutine
}
my $sum = add(5,6);
由于最后一条语句是say
函数,它隐式返回一个布尔值,因此会抛出一个错误:
11
Type check failed for return value; expected 'Int' but got 'Bool'
in any return_error at src/vm/moar/Perl6/Ops.nqp:649
in sub add at test.p6:5
in block <unit> at test.p6:8
打印预期答案,无任何错误:
11
定义退货类型更清晰的方法是使用退货(谢谢):
我还想说,还有一种编写它的替代方法subadd(Int$,Int$)返回Int{…}
11
sub add (Int $inputA, Int $inputB) returns Int
{
my $result = $inputA+$inputB;
return $result;
}
my $sum = add(5,6);
say $sum;