Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

[parrot] parrot prototype OO?

by jepri (Parson)
on Feb 05, 2008 at 16:39 UTC ( #666342=perlquestion: print w/ replies, xml ) Need Help??
jepri has asked for the wisdom of the Perl Monks concerning the following question:

So I sat down to see if I could do some prototype style programming in parrot (my goal was to see if I could implement a prototype-style system in PIR). It didn't go well.

At first I thought to simply subvert the classes and use them as objects, but there are no class methods, so that was out. Then I thought I might try to mess with the metaclass, which does appear to have "class methods", but when I cloned it I couldn't seem to make one the parent of the other.

So it seems I'm left with writing my own prototype PMCs (ouch), writing some complex objects to proxy calls, or throwing the whole lot away and doing it all with closures. I might have missed a trick, but PDD15 gets prototypes slightly wrong*, so I suspect there really is no good way to do it right now.

Does anyone have some hot ideas on how to arrange inheritance between objects in parrot? Or to give classes methods?

* It states that prototype systems have no classes, but skips over the fact they still have inheritance. A better way to put it might be that prototype systems don't have objects.

(Edit) References:
Prototypes explained
PDD15
Prototypes in Perl5

___________________
Jeremy
I didn't believe in evil until I dated it.

Comment on [parrot] parrot prototype OO?
Re: [parrot] parrot prototype OO?
by chromatic (Archbishop) on Feb 05, 2008 at 19:08 UTC

    It's difficult to say what's going on without seeing code, but if I were to do this, I'd write a Prototype PMC in PIR that subclasses the Class PMC and overrides find_method(). You can also override its add_parent() when you clone an instance, so that the new instance can look back to its parent for non-overridden methods.

      There's not a lot of code - I was just trying parrot as-is, using only the provided PMCs. Thanks for the tip on PMCs, I wasn't sure if it were possible to write a full PMC in PIR.

      Now that I know rolling my own PMCs is the way to go, I'll go that way. Could you suggest an example of a PMC written in PIR? I'm only finding ones written in C.

      Edit: I just found your FizzBuzz article, working through that now.

      ___________________
      Jeremy
      I didn't believe in evil until I dated it.

      Hmmm, find_method seems to be called on the superclass, and not the class (or on the class and not the object). So if I try to use, say, objects, should I return a method that inspects the object (and its parent* slots) and then calls the correct method? Because that sounds like I'm implementing AUTOLOAD.

      Here's my naive code. I don't seem to be making good use of the class machinery so far:

      .namespace [ 'MyStuff'; 'TestProt' ] .sub 'autoload' :method #inspect object and parents here .end .sub 'find_method' :vtable .param pmc arg0 .param pmc arg1 .local pmc autoload new_method = find_name 'autoload' .return(new_method) .end .sub main :main $P1 = new [ 'MyStuff'; 'TestProt'] $P1.hi() .end

      ___________________
      Jeremy
      I didn't believe in evil until I dated it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://666342]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2014-07-13 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls