Getting started with Io
Io is a prototype-based langauge that has been around since 2002. What stood out to me about it is that it has a great deal of flexibility despite its remarkably simple syntax.
The easiest way to get started with Io is to play around with it on the REPL. Download the binary for your OS from iolanguage.org (you can do brew install io
on OSX) and start the repl with the io
command.
In the interest of following the traditions of programming language tutorials, let me start off with the Hello World example:
Prototype and objects
Io is a prototype-based language, so it doesn’t have classes. Instead, you get prototypes, which are objects that you can clone to create new instances of objects. As a result, prototypes can fill the role of both classes and objects. Let’s see how this works.
Object
is the parent of all prototypes. You can create your own Dog
prototype by cloning Object
.
Objects in Io are made up of key-value pairs called “slots”. We can access the list of slots for an object using slotNames
. We can also find the type of a prototype using the type
method. Let’s see how we can set a slot called “description” for the dog.
Because the name of the prototype here starts with an uppercase character, this Dog
prototype acts in a similar way to classes in class-based languages. When you clone a prototype, Io sets the type
slot if it starts with an uppercase character.
Creating instances
If Dog
is equivalent to classes in class-based languages, how do we create instances? Let’s clone Dog
to create an instance of it.
From the above snippet, you can see that when you create an instance of Dog
, the type is Dog
- the name of the prototype from which it was created. You can also see that no slots are set on the object, but you can still access the description slot you set on Dog
.
If a slot is not found on the object, it looks for the slot in the parent object, and keeps checking until it gets to Object
. Let’s try setting a new slot on the dog object.
You might have noticed in the above code that I’m using two different types of assignment operrators. The :=
operator is used to set a new slot, whereas the =
operator updates an existing slot. Once these slots have been set, you can see that they are present by passing the slotNames
message to dog
.
Methods
Let’s look at how you can define a method. Here we’re defining a greet
method on the Dog
prototype.
If you want to define a method that accepts arguments, you can define it like this:
Lists
The way lists work in Io is pretty neat. Let’s look at what we can do with a list of numbers:
Control flow structures
Let’s take a quick tour of the control flow structures that are available in Io.
Final thoughts
I have only looked at the basic features of the language here. So far, I’ve been really impressed with how much flexibility it offers despite the simplicity of the syntax and the object model.
One big problem with Io is how hard it is to Google for it, so finding online resources for it is pretty hard. I came across the language in Bruce Tate’s excellent Seven Languages in Seven Weeks book, but after the book, I’ve found very little stuff online except for the guide and docs on the language website.
Io has some interesting concurrency features, but I haven’t explored it much yet. Hopefully that will turn into another post here. In the meanwhile, you can read more about Io by following the links below.