4 ms·
I'm having to reach back into the dustbin of my mind, but I seem to recall implementing an object system using only closures at one point. As I recall, it had a
by aethertap 14y ago
I'm having to reach back into the dustbin of my mind, but I seem to recall implementing an object system using only closures at one point. As I recall, it had all of the "normal" features of objects - inheritance, member variables, methods, etc. It was done with closures and the members were accessed in a message-passing style (in scheme, (myobject 'show) for example).
Based on that, I think that closures and objects are probably equivalent in their expressive ability. Could be wrong though, like I said it was a long time ago.
- acjohnson55 14y agoInheritance can be a bit tricky to fit in, as I recall, but it's quite elegant to implement basic classes and objects with just closures.
- pjscott 14y agoA very simple and elegant way to handle inheritance is to give each object a pointer to another object -- let's call it "prototype" -- and delegate any failed member variable or method lookups to the prototype. This is how JavaScript's object system works.
- twoodfin 14y agoI'm pretty sure this is an extended discussion/exercise in SICP.