Objects, Messages, Scheme
Posted by Peter on March 19, 2006
I have had a love-hate relationship with object oriented programming for sometime now. When I started programming with C++ I loved objects, probably because objects were the only way to get interesting things done. A lot of the “problems” that I was solving with inheritance and polymorphism could have been accomplished much more simply with a little functional programming. When I realized this objects fell out of favor, although I have always felt that there was something elegant about them. Recently a post on io by José Ruiz caused me to think again about objects in programming.
The beauty behind objects in languages like io and Smalltalk is not that they can be inherited, polymorphic, ect, but the system of messages. Messages are the ultimate generalization away from the specific implementation of a type. All objects can receive messages, and there are no restrictions as to what that message can be. Of course in most implementations trying to call a message that the object doesn’t support results in an exception or some other form of error, and this is probably the behavior you want. Another interesting feature of these messages is that it is easy to generalize a multi-threading model from them. In such a system each object has a message queue, and the system rotates between objects attempting to process whatever is on top of their queue.
Fortunately in Scheme it is easy to implement a message based object system. An object is defined as a function that takes one or more parameters. The first parameter is the message (usually a symbol), and when the object/function is called that symbol is looked up in an internal dispatch table, which then applies a stored procedure to the remainder of the arguments. The reason that object systems in Scheme don’t remain this simple for long is that we want more from them, such as the ability to define prototypes for types of objects, inherit from existing types, ect.
Here is a brief list of features that I plan on playing around with when implementing an object system in Scheme:
Inheritance, single and multiple, virtual and non-virtual
Copying of existing objects
Adding messages and members to an object at run-time
Static data shared between objects of the same type
Explicit casts between one type and another
Obviously I will write more on this later, but I wanted to get some of my initial thoughts down before I went ahead with anything.