Ok, this is basically a repost of a part of Re^3: [Perl 6] Object methods on the fly?, in which I probably asked too many further side questions, all of which OT wrt the main thread. So I'm asking again here in the hope that it will have more visibility...

Taking as a basis the code TimToady posted in Re^2: [Perl 6] Object methods on the fly? just adding a my to make it a declaration:

my @array does role { method insert ($x) { @.push($x) unless any(self) eqv $x; } };

I though it would been nice to also initialise it, which is possible as I found rather than reading the docs as I probably should have, by trial and error under pugs:

my @array does role { method insert ($x) { @.push($x) unless any(self) eqv $x; } } = 1..5;

But to be fair this was not the first attempt, the latter really being with the assignment directly to the right of @array, which didn't work. OTOH although I can understand why the other way it's more consistent and why the proposed way it would be harder even to parse, or necessarily require parens, for one thing, I'm also sure most people would find it to be more intuitive, while as it is now I suspect most would just say: "for clarity, initialize it in a separate statement."

Replies are listed 'Best First'.
Re: [Perl 6] Initialization within complex declarations
by TimToady (Parson) on Jul 25, 2007 at 00:18 UTC
    You're probably just running into a precedence issue. Parentheses will help unless it's also a pugs bug. In any case, you can't do does directly after the assignment because it would apply to 1..5, not the entire declaration, unless the declaration slurps the pseudo-assignment up at a higher precedence than it ought. Arguably we shouldn't be using = for pseudoassignment, but it's pretty engrained in the culture by now.

      Arguably, we shouldn't be using = for mutating assignment (set), only for functional assignment (let). We can use <- or := or ! for mutation. It's pretty engrained in the culture by now though, because of FORTRAN and C.

Re: [Perl 6] Initialization within complex declarations
by ambrus (Abbot) on Jul 24, 2007 at 09:04 UTC

    As I'm a C++ guy, I always shiver when anyone says "for clarity, initialize it in a separate statement". That's the kind of thing that often works, but if you get used to it, you'll have a hell of time to debug when it fails. (That's the reason why the C-style for loop has a wierd syntax extension in C++ so you can declare and initialize the loop variable at the same place.)

    Of course, perl's = is not the same as C++ =.

      As I'm a C++ guy, I always shiver when anyone says "for clarity, initialize it in a separate statement".

      Yeah, I'm not a C++ guy, but I second that and that's precisely the reason why I started this thread: I'm a Perl guy, and I think that an alternative syntax that would give improved visibility of the initialization bit would be nice, it would be very perlish. But then maybe I'm just going mad around a corner case.

Re: [Perl 6] Initialization within complex declarations
by moritz (Cardinal) on Jul 25, 2007 at 11:46 UTC

    My first law of Perl6 learning: Though shalt not conclude anything from the implementations.

    Perl 6 is quite a complex language to implement, so you should really consult the specs.

    Sadly, pugs didn't have many commits latetly, due to audreyt being not too well (and a few other reasons), and doesn't parse everything that the specs allow, and doesn't reflect the newest changes to the synopsis.

    Since things like my $a is foo = 3; are allowed (where foo is an arbitrary trait), I guess your example should work.