On Sun, Sep 21, 2008 at 07:02:37PM -0700, Michael G Schwern wrote:
> I'm pondering what the proper syntax is for a subroutine parameter with both a
> trait and a default. That is...
> sub foo ($arg = 42)
> sub foo ($arg is readonly)
> together in one parameter. Would that be
> sub foo ($arg = 42 is readonly)
> sub foo ($arg is readonly = 42)
> The first looks ambiguous, what if the trait is meant to apply to the default?
> The second looks downright wrong.
The STD.pm grammar  shows that the second is the correct form --
i.e., default values occur after traits. (See "token parameter"
on or about line 2630.) I think part of the reason for this is that
traits appearing after the default value would be applied to the
default value instead of the parameter. A few other examples from
the synopses seem to confirm this pattern:
S03:2289: my Dog $fido is trained is vicious = 1
S03:3828: constant Dog $foo is woof = 123;
S06:1558: constant $pi is approximated = 3;
S12:1393: has SomeType $.prop is rw = 1;
> PS Incidentally, it seems silly to have "is rw" but not "is ro". I keep
> writing "is ro".
Yes, we've also run into this problem a few times while working on Rakudo.