Mike Austin's Blog

Saturday, December 09, 2006

Prototype based languages

Prototype based languages are really nice when you want to share data across instances, but have the option to override that data.

For example, in Io you can write:
Button := Object clone do (
background := Color clone set(0.5, 0.5, 0.5)
)

button := Button clone
button background = Color clone set(0.75, 0.75, 0.75)
If you don't assign button a color, it will look for it in the prototype. It's possible to do this in Ruby with 'class instance variables', but it's not as pretty :)
class View
class << self
attr :background, true
end

@background = [0.9375, 0.9375, 0.9375]

def View.inherited( subclass )
subclass.instance_variable_set(
'@background', [0.9375, 0.9375, 0.9375] )
end

def background()
if @background
@background
else
self.class.background
end
end
end