Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Thursday, June 08, 2006

Classes are Just a Prototype Pattern

My friend Dave Fayram (who helped bring advanced LSI classification to Ruby’s classifier) has heeded Matz’s advice to learn Io and is bringing me with him. I have been thinking a lot about prototyped versus class-based languages lately and once I really understood it, I fell in love. I have a feeling I will be writing a lot about this topic, but here is a brief introduction.

# Class-based Ruby

class Animal
attr_accessor :name

# A class can be instantiated
amoeba = Animal.new
amoeba.name = "Greenie"

# A new class needs to be defined to sub-class
class Dog < Animal
def bark
puts @name + " says woof!"

# A sub-class can be instantiated
lassie = Dog.new
lassie.name = "Lassie"
lassie.bark # => Lassie says woof!

Notice in the Io version that you never ever define a class. You don’t need to.

# Prototype-based Io

Animal := Object clone

# An object can be instantiated
amoeba := Animal clone
amoeba name := "Greenie"

# An object can be used to sub-class
Dog := Animal clone
Dog bark := method(
write(name .. " says woof!")

# An object can be instantiated
lassie := Dog clone
lassie name := "Lassie"
lassie bark # => Lassie says woof!

You will notice some syntactical differences immediately. First, instead of the dot (.) operator, Io uses spaces (note: technically, with a couple lines of Io you can actually make Io use the dot operator or the arrow operator (->) or anything else you would like).

Next, you will notice that instead of making a new instance of a class, when you use prototype-based languages you clone objects. This is the foundation of prototyping… defining classes is unnecessary, everything is just an object! Furthermore, every object is essentially a hash where you can set the values of the hash as methods for that object.

You should follow me on twitter here.

Technoblog reader special: click here to get $10 off web hosting by FatCow!


Anonymous Anonymous said...

Hrm..javascript is a prototyped language, did you not notice?

6:10 PM, July 24, 2006

Anonymous Anonymous said...

And ecmascript is loosely based on Self (which was written as a simplification of Smalltalk), the language that lost to java in Sun :( ... AinĀ“t history peculiar?

10:55 PM, April 20, 2007

Anonymous Anonymous said...

hey ;)
IMHO you should read about JavaScript inheritance technics. You will probably find, like many have, that prototypes are not types (classes). You should also understand that prototypes are meant for untyped languages (also not type-safe). Dig further ;)

2:41 PM, June 30, 2008

Anonymous Anonymous said...

... check this out:

have fun ;)

2:45 PM, June 30, 2008

Blogger Lucas Carlson said...

IMHO you should read my article before commenting... I never said that prototypes are classes, I said you can model classes within a prototype based system.

2:50 PM, June 30, 2008


Post a Comment

Subscribe to Post Comments [Atom]

<< Home


If you like this blog, you might also like top photography schools.