Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to provide named parameters with a hash in Perl 6?

Tags:

raku

How to do it correctly?

> 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
like image 377
Eugene Barsky Avatar asked Jan 13 '18 21:01

Eugene Barsky


1 Answers

Add a prefix | to %param

adding( |%param )

This also works for positional arguments

sub foo ( $bar, $baz ){ "$bar:$baz" }
my @args = 'a', 'b';
foo( @args ) # error
foo( |@args ) # "a:b"
like image 194
Brad Gilbert Avatar answered Nov 15 '22 11:11

Brad Gilbert