develooper Front page | perl.perl6.language | Postings from March 2005

compile time signature checking

Thread Next
From:
Abhijit Mahabal
Date:
March 2, 2005 21:58
Subject:
compile time signature checking
Message ID:
20050303055811.23218.qmail@lists.develooper.com
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

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About