On Tue, Aug 23, 2005 at 03:48:03PM -0400, Sam Ruby wrote: : How will Perl6 evaluate defaults? : : Like Python: : : global x : x=1 : def f(p1=x): : return p1 : x=2 : print f() : : or like Ruby: : : $x=1 : def f(p1=$x) : return p1 : end : $x=2 : puts f()
By default, delayed like Ruby, but you can easily force earlier evaluation if that's what you want. In general, pseudo-assignment to declarators is special-cased to happen at the time appropriate to the scope and lifetime of the declarator, and formal parameters are no exception. Basically, the container constructor is passed the closure of what's on the right of the =, and it can choose to evaluate that closure whenever it wants. For instance, state $s = $x; sets $s to the current value of $x only the first time it's executed. So while formal parameter defaults execute "as late as possible", and state variable initializations run "just in time", object attribute defaults actually run "right away", that is, class declaration time (aka "BEGIN" time), and you have to add braces to delay evaluation till object init time. Arguably that's a little inconsistent, but the inconsistency can be viewed as implicit to the declarator, not the pseudo-assignment syntax, which is just syntactic sugar for a closure. Larry