Self is wonderful and beautiful, and that "Power of Simplicity" paper
is excellent reading, a true classic.
Like with Lua, you can also roll any kind of object system in Self, but
it's excellent for making prototype based object systems (and making
them run fast, albeit by using lots of memory).
That paper covers some original techniques that can be applied to
just-in-time compilers for many other oops like Smalltalk, Java and
Lua: aggressive inlineing, polymorphic inline cache, virtual classes
(they're prototypes in the programmer's mind, but Self optimizes them
to virtual classes behind the scenes) dynamic de-optimization
(pessimization :) for debugging, etc. The people and technology behind
Self led the way to Sun's "HotSpot" just-in-time compiler.
Another powerful approach to prototype based oop is OpenLaszlo's
"instance first development", as described by Oliver Steele (who
managed the Dylan project at Apple, and designed OpenLaszlo):
http://osteele.com/archives/2004/03/classes-and-prototypes
http://c2.com/cgi/wiki?InstanceFirstDevelopment
http://www.openlaszlo.org
"
LZX
is a
prototype-based
language:
any attribute that can be attached to a class definition, can be
attached to an instance of that class instead. This is handy in UI
programming, where there are a number of objects with one-off
behaviors. It’s also handy in prototyping and incremental program
development, where it creates the possibility for a novel kind of
refactoring."
"The equivalence between the two programs above supports a development
strategy I call
instance-first development.
In instance-first development, one implements functionality for a
single instance, and then refactors the instance into a class that
supports multiple instances."
"In instance-first development, one implements functionality for a
single instance, and then refactors the instance into a class that
supports multiple instances."
"Many
PrototypeBasedProgramming
languages don't obey the instance substitution principle, either
because they don't have classes, or because class and instance
definitions are not parallel. (Typically there's not a declarative
means for defining an instance member.)
_javascript_
versions 1.0 through 1.5 (the versions in browsers) is also a
prototype-based language, but lacks classes as a first-class syntactic
entity, and lacks the hierarchical syntax that Java, C++, and
OpenLaszlo use to define
class members.
_javascript_
2.0, JScript.NET, and Python have a class definition syntax, but don't
use the same syntax to define instance members. For example, contrast
the following two Python programs, which parallel the
OpenLaszlo programs
above."
"The syntactic version of the instance substitution principle makes a
class look like a function or a macro. Class, function, and macro
definitions are all mechanisms for abstracting program structure so
that it can be reused."
-Don
Roberto Ierusalimschy wrote:
[
OK, I understand that I can implement class-based OOP in Lua. Can you
provide any analysis on when prototypes are better or worse than
classes?
I think the papers about Self (the first language to propose
prototype-based OO) are a good reading (although of course their
analysis is somewhat biased). The classic "Self: The Power of
Simplicity" is a good start:
http://research.sun.com/self/papers/self-power.html
-- Roberto