Joseph Ryan writes: > Dan Sugalski wrote: > > >On Mon, 25 Aug 2003, Joseph Ryan wrote: > > > > > > > >>So, I know how to use find_method to get a method from an object; > >>but is there any way to dynamically add a method to a class? > >>Basically, I want to do something like this: > >> > >> newclass P2, "Foo" > >> new P1, P2 > >> > >> addr I0, _Foo::somemethod > >> setmethod P1, "somemethod", I0 > >> findmethod P0, P1, "somemethod" > >> invoke > >> > >>So, how do I do it? :) > >> > >> > > > >What's supposed to happen is that each class has a backing namespace, and > >methods all live in that namespace. Generally objects, no matter what > >their HLL class, will be PMCs that have subclassed (at the parrot level) > >ParrotObject. > > > >Anyway, for a perl/python/ruby object of class Foo, to add a new method > >you'd just add a new sub/method name/PMC binding to the Foo namespace. > > > > I'm a bit lost here; what does that mean? How do I do it? Does > > .sub _Foo::somemethod > print "Foo-ey goodness." > .end > > add "somemethod" to the "Foo" namespace?
No... well, imcc will probably support that eventually. It would be (note the conditional; i.e. you can't do this object stuff yet): .sub _main newsub $P0, .Method, __foo_somemethod global "Foo::somemethod" = $P0 ... .end .sub __foo_somemethod print "Foo-ey goodness." .end If C<global> is smart. If not: .sub_main newsub $P0, .Method, __foo_somemethod $P1 = global "Foo::" $P1["somemethod"] = $P0 ... .end Note that __foo_somemethod is a completely arbitrary name. It could have been called _boozebar for all these ops care. Luke > Or would it have to be: > > .sub Foo::somemethod > print "Foo-ey goodness." > .end > > Thanks for the reply, > > - Joe >