[Ficl-developers] Bound Instance Methods HOWTO
Brought to you by:
jsadler
From: David M. <da...@re...> - 2004-07-17 22:42:52
|
Hi, As someone who's written lots of Python, I've grown very fond of bound instance methods - ie, a single entity which, when executed, invokes a method of a class in respect of a specific class instance. For instance: class foo: ... def bar(self, *args): print "this is foo.bar" fooInst = foo() x = fooInst.bar # this saves a 'bound instance method' ... x() # equivalent to 'fooInst.bar()' I couldn't find anything similar in FICL's OO, so decided to implement it myself. Here's the code -------------------- also oop definitions \ helpers for -->bind-xt-for-method : (exec-instance-method) ( xt-thisword ) dup 2@ ( xt-thisword cls inst ) rot 2 cells + @ ( cls inst xt-clsMeth ) execute ; : (bind-instance-method) ( inst cls xt-clsMeth -- xt-instMeth ) \ create a word to hold the 'inst cls xt', and run the method s" create _im_" evaluate ( inst cls xt-clsMeth ) -rot ( xt-clsMeth inst cls ) , , , ( ) last-word ( xt-instMeth ) does> \ ." instance method runner started" cr (exec-instance-method) ; \ the beast itself \ binds an instance method to an xt, so whenever the \ xt is executed, the object signature ( inst cls ) is \ pushed onto the stack, and the method executed : -->bind-instance-method ( inst cls -- xt ) state @ if \ compiling parse-method ( inst cls addr u ) postpone lookup-method ( inst cls xt-clsMeth ) postpone (bind-instance-method) ( xt-instMeth ) else \ interpreting parse-word ( inst cls addr u ) lookup-method ( inst cls xt-clsMeth ) (bind-instance-method) ( xt-instMeth ) then ; immediate ----------------------------------- Now, for an example: -------------------- also oop definitions object subclass c-myclass cell: .somestuff : setstuff { x 2:this } ." setting somestuff to " x . cr x this --> .somestuff ! ; : showstuff { 2:this } ." somestuff = " this --> .somestuff @ cr ; end-class c-myclass --> new myinst 44 myinst --> setstuff myinst --> showstuff \ make 2 instance method bindings against instance 'myinst' myinst -->bind-instance-method showstuff constant showstuff-myinst myinst -->bind-instance-method setstuff constant setstuff-myinst showstuff-myinst execute 56 setstuff-myinst execute showstuff-myinst execute --------------------------------------- As you can see, we've created single xt's which each invoke a particular method of a particular instance of a particular class. This can be handy, especially when working in with other code that needs single xt's. There is a cost: * a new dictionary word gets created for each bound method of each instance. so if you're creating bindings for 3 methods of a class, and you have 20 instances of the class, that's 60 words! however, each word stores only 3 data cells and 2 instructions (including the semiParen) * on execution, there's an overhead of 11 (albeit quick) instructions per invocation Lastly - I've tested -->bind-instance-method in both compilation and interpretation states, and it seems to work fine in both. Enjoy (if there's actually anyone reading this) -- Cheers David |