Wednesday 26 May 2010

Objects and Recursion

I met William Cook last year at OOPSLA 09 (renamed SPLASH as of this year) whose presentation and subsequent blog entry "objects use recursion everywhere" reminded me of work I did years ago on a pictorial representation of how recursion occurs in objects due to 'self' and 'inheritance' (leading to a colleague at the time commenting on how it reminded them of a Klein Bottle).
The basic idea is that an object is a rod of methods surrounded by an environment of variable bindings as shown in cross section on the left. All methods can reference 'self' which can be shown as a hole that is drilled through the methods rod:

At this stage, the 'self hole' is waiting for an object to be supplied. Any object that conforms to the required interface can be supplied. Recursion occurs because the object that is used to fill the hole is the object itself...



...this is achieved by grabbing the top of the object, pulling it round and feeding the object into its own 'self hole'...
grab the top ... ...pull it round...          



...feed the object in and keep going...

This trick can be be used to explain super too. Instead of onw hole there are two holes:

Inheritance means fitting rods together:

This idea was used to define a semantics for Smalltalk. The details can be found here but beware, it is a scanned document and is over 60MB. This slightly less esoteric (but a similarly large scanned document) set of notes on OOP and recursion is a useful introduction.

No comments:

Post a Comment