in reply to Re^2: [Perl 6] Object methods on the fly?
in thread [Perl 6] Object methods on the fly?

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

Whoa! And it evens runs on Pugs:

pugs> my @array does role { ....> method insert ($x) { @.push($x) if none(self) eqv $x } ....> } () pugs> my @array=(1..5); pugs> @array.insert($_) for 1,2,6; pugs> say @array 123456 Bool::True

BTW: Could I put the initialization in the definition too? I tried with

pugs> my @array does role { ....> method insert ($x) { @.push($x) if none(self) eqv $x } ....> } = (1..5); (1, 2, 3, 4, 5)

and it works, but it doesn't with the assignment directly to the right of @array, OTOH I'm sure most people would find it to be more intuitive, while this way they would just say: "for clarity, initialize it in a separate statement."

BTW: why doesn't the following work, instead?

pugs> my @array does role { ....> multi method insert ($x) { @.push($x) if none(self) eqv $x } ....> multi method insert (@x) { @.insert($_) for @x } ....> } = 1..5; (1, 2, 3, 4, 5) pugs> @array.insert(3..7); pugs> say @array 1234534567 Bool::True

(If I use *@x, pugs "hangs", instead.)

BTW: (the last, really!) what is self supposed to be? After all no suitable and short enough variable/pronoun was found? The following is all that pugs can tell me:

pugs> self macro {Prim ([Pugs.AST.Internals.Val] -> Pugs.AST.Eval.Eval Pugs.AST.I +nternals.Val)}

Update: Two of the questions asked here were reposted in separate new threads in

Replies are listed 'Best First'.
Re^4: [Perl 6] Object methods on the fly?
by TimToady (Parson) on Jul 25, 2007 at 00:42 UTC
    self is just a built-in function returning the invocant. If you want a different name for it, you can always declare it explicitly.

    Also, it's probably bogus to say any(self). It should probably be a function that is not context sensitive, in which case we'd have to say any(self[]) or some such to pull out the elements.

    Finally, I think the failure of the multi form is just hitting some things that aren't completely implemented in pugs yet.