Those of you who know me well know that I’m a fan of languages. You could say this is one of my defining characteristics— right next to my addiction to caffeinated beverages
Provided a language meets a small set of criteria (cross platform, free) and has some unique characteristic(s) that catch my attention, I’ll give it a look. To do otherwise would just be narrow-minded (which is the same thing as being self-destructive, as a developer).
Io is just such a language. I’ve run into it a few times over the last few years, and as I’ve noticed an increase in interest in this fairly young language (especially by fellow Rubyists), I thought I’d show it some attention.
In this article I’m just going to discuss the very basic concepts of prototypes
Classless is Classy
Io can be a bit of a shock for those that haven’t played with other prototype languages like Self; we’re talking about a classless OO language here. Objects are just that— objects. The idea that there are special “blueprints” to an object simply doesn’t exist as a first-class concept.
Is this a class?
Car := Object clone
No, Car here is just an object, and so is Object. We can treat this incarnation as a class (cloning more objects from it), or we can just add data and logic to it for immediate use. For this example, let’s show how it can be used as a class, since we’ll want to have a whole autobahn of cars.
Slots are just that
, Objects have “slots,” which, essentially, are the list of messages that it responds to. “Car” in this example, is a slot in the Lobby object, the object our program is running inside. We could have written it Lobby Car := Object clone, instead.
Now let’s add a few slots to Car. These simple ones are somewhat analogous to attr_accessor:
Car newSlot("make")
Car newSlot("model")
Car newSlot("mpg")
Which just create blank slots for “make” and “model.” We can also assign slots directly. Let’s pretend all cars come with a full 16 gallon tank of gas, and there’s nothing on the odometer.
Car fuel := 16 Car milesDriven := 0
A slot is just a slot, and can also hold a method. Let’s make this baby roll. The syntax might seem odd at first, but you’ll pick it up.
Car drive := method(miles,
fuelToUse := miles / mpg
if(fuelToUse > fuel,
"Fill 'er up first" println
false
,
milesDriven = milesDriven + miles
fuel = fuel - fuelToUse
"Miles Driven: #{milesDriven}, Fuel: #{fuel}" interpolate println
true
)
)
Something worth mentioning is the fact that the name Io, while short and sweet— and a name I particulary like— can make google searches for material pretty tricky. I recommend io language as a search term instead of the more false positive-prone io.