I was thinking about how binding of arguments to parameters in a sub/method call would happen. Seems to be a darn tricky thing with all sorts of potential pitfalls!

I have a few questions. Consider the following piece of code. Are my expectations correct?

sub foo($x, $y, *%slurp) { $x + $y }
say "expecting 10:  ", foo(3,7);
say "expecting 10:  ", foo(x => 3, y => 7);
say "expecting 10:  ", foo(y => 7, x => 3);
say "expecting 10:  ", foo :y<7> :x<3>;
my $c = "x"; my $d = "y";
say "compile time error?:  ", foo($c => 3, $d => 7);
say "maybe syntax error? perhaps not  ", foo :$c<3>  :$d<7>;
say "run time error?   ", foo(x => 3, y => 7, $c => 5);

Similarly, what happens here?

class foo{
  has $.x;
  method bar($.x){} # implicit *%_
}
my $f = foo.new;
my $c = "x";
$f.bar(10, $c => 5); # runtime error?


Finally, does this also mean that defaults may not be applied until run time? For example:
sub foo(?$x = 3){...}
foo($c => 5); # $c may be "x", in which case default not needed.


--abhijit

Reply via email to