## Prototype based OO

After doing C++ for years, with some Java/C# here and there, I've gotten around to doing some web development and have started playing around with Javascript. With the recent Ajax and "web 2.0" hype, Javascript seems to be getting more attention.

I've been curious about Prototype based Object-oriented languages for a little while now, but have never used one extensively as modern, widely-used implementations (except for Javascript) seem to be rather scarce. Slate seems to be still in development though. Here is a LtU discussion that concentrated on memory concerns and here is a paper on Self.

What are others thoughts on OOPLs. Is the added flexibility that proponents argue needed to be looked at more? What are the pitfalls and advantages? Do languages like Ruby "fake" it well enough? I'd be particularly interested in the psychological criticism given in this (google cache)paper, as well as this paper.

## Comment viewing options

### Couple of thoughts

Lua is also worth mentioning as a prototype OO language that's had pretty good success.

As for JavaScript, the 2.0 recommendations seem to be moving away from prototype based OO and into a class based solution (causing concern for some about the future direction of the language).

### Lua

Yeah, I had forgot about Lua. It looks like they're adding classes, but not taking away prototypes.

### Ruby "fakes" it?

Ok, I'm a bit puzzled by what you mean here.

### Faking it

It looks like my original comment regarding Ruby features "faking it" got edited out at some point during my post.

What I mean is does open classes, mixins, duck typing and the overall dynamic nature of Ruby mitigate some of the perceived static feel to class based OO. I believe Ruby itself does have the mechanisms for doing prototype OO, but I don't think it's used all that much.

### Io like prototypes in Python

I've once written a prototype Object in Python inspired by Io. Note that the semantics of the Python prototype is simpler in some respects ( no multiple inheritance, no super call ) but is also more elaborated with respect to freeze/thaw. I introduced the latter because I was not glad about modifying attributes of prototype clones by modifying the attributes of the prototype itself. Freezing means that once a prototype is ready for cloning it may not be modified anymore. This is the default behaviour. Thawing means revoking the frozen state of the prototype.

class Object(object):
def __init__(self):
self.__dict__["_parent"] = None
self.__dict__["_frozen"] = False

def freeze(self):
self.__dict__["_frozen"] = True

def thaw(self):
self.__dict__["_frozen"] = False

def clone(self):
obj = Object()
obj._parent = self
self.freeze()
return obj

def __getattr__(self, name):
try:
return self.__dict__[name]
except KeyError:
return getattr(self._parent,name)

def __setattr__(self, name, value):
if self.__dict__["_frozen"]:
raise AttributeError,"object is frozen"
self.__dict__[name] = value

>>> a = Object()
>>> a.f = lambda x: x+1
>>> a.x = 7
>>> b = a.clone()
>>> a.c = 0
Traceback(...)
...
AttributeError: object is frozen
>>> b.c = 0
>>> b.f(b.c)
1


Kay

### Mechanism for doing prototype OO

ruby has "singleton methods" which allow you to redefine a method for a single object.
Actually, I agree they're not used very much, except in the Class way: each Class is an instance of the Class class but it has a singleton method for each class method

### IO and Prototype List

I like the Io language and its author, Steve Dekorte, has also created a list of Prototype based languages.

### References

Does anyone know of a good book on prototype based OO development? I'm more interested in how it impacts actual programming and design rather than in language specification and implementation issues. Of course links to papers would also be very welcome.