是否可以在Perl 6中提供带有哈希的命名参数?

时间:2018-01-13 21:34:29

标签: perl6

如何正确完成?

> sub adding(:$first, :$second) { $first + $second }
> adding(second => 2, first => 1)
3
> my %param = second => 2, first => 1
{first => 1, second => 2}
> adding(%param)
Too many positionals passed; expected 0 arguments but got 1
  in sub adding at <unknown file> line 1
  in block <unit> at <unknown file> line 1

1 个答案:

答案 0 :(得分:12)

|

添加前缀%param
adding( |%param )

这也适用于位置参数

sub foo ( $bar, $baz ){ "$bar:$baz" }
my @args = 'a', 'b';
foo( @args ) # error
foo( |@args ) # "a:b"