Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: [Perl 6] Object methods on the fly?

by TimToady (Parson)
on Jul 18, 2007 at 18:27 UTC ( [id://627329]=note: print w/replies, xml ) Need Help??


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

You're on the right track. You want something like:
@array does role { method insert ($x) { @.push($x) unless any(self) eqv $x; } }
Arguably we could provide syntactic sugar to reduce that to:
@array does method insert ($x) { @.push($x) unless any(self) eqv $x; }
Perhaps that use of does is redundant with but= and could be removed, but it does read better, I think. In any case, doing an operation like this on an object ends up creating an anonymous class that derives from the original class and adds in the extra method, so you don't clobber the original Array class.

Replies are listed 'Best First'.
Re^3: [Perl 6] Object methods on the fly?
by blazar (Canon) on Jul 19, 2007 at 20:29 UTC
    @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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://627329]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found