Io Day One
December 12, 2013
To get started playing with the language, I installed the interpreter using Homebrew with
brew install io.
- Things to find
- Some Io example problems
- An Io community
- A style guide with Io idioms
- Questions to answer
- Type system
- Truthy and falsey values
- Slot compatibility
- Assignment operators
- Things to do
- Run an Io program from a file
- Executing methods
Things to find
The Io website documents language clearly and concisely, so finding what you’re looking for isn’t all that difficult.
Some Io example problems
It seems there were some example problems in the Io website, but they’ve since gone missing. I managed to find an archived version, and I copied them to this gist.
An Io community
The Io community can be found in all of the usual places:
There’s also a mailing list, if you’re into that kind of thing.
A style guide with Io idioms
I found a brief style guide on Wikibooks. I suppose given such a spartan syntax, there isn’t much more style one could guide.
There’s a good deal of further reading on the io-fans website, though each article is maintained by an individual and could go out of date or disappear entirely.
Questions to answer
Evaluate 1 + 1 and then 1 + “one”. Is Io strongly typed or weakly typed? Support your answer with code.
Io is strongly typed. Running the above through the interpreter returns
1 + 1, and throws an exception when adding the number to the string. To me, this is expected behaviour. I doubt I would ever want the consecutive letters o-n-e to be interpreted as the number
1 by a scripting language. I might want the string
"1" to be interpreted as the number
1, but Io throws an exception there too.
2, but if one of those numbers had been a string, the
+ operator would have performed concatenation instead of addition, i.e.,
1 + "1" evalutes to
"11" — which is probably not what you wanted!
Truthy and falsey values
0true or false? What about the empty string? Is
niltrue or false? Support your answer with code.
As far as I can tell, it’s not possible to explicitly cast a value to boolean. Truthy and falsey values can be discerned by sending the
Io> 0 isTrue ==> true Io> "" isTrue ==> true Io> nil isTrue ==> false
How can you tell what slots a prototype supports?
If you’re just looking for a list of slots, you send the
slotNames message. If you’d like a map of names and values, you send
Io> Car := Object clone ==> Car_0x7ff3590b27f0: type = "Car" Io> Car model := "Boxster S" ==> Boxster S Io> Car slotNames ==> list(model, type) Io> Car slotSummary ==> Car_0x7ff3590b27f0: model = "Boxster S" type = "Car"
What is the difference between
:=(colon equals), and
::=(colon colon equals)? When would you use each one?
The documentation covers this very well already.
::=Creates slot, creates setter, assigns value
:=Creates slot, assigns value
=Assigns value to slot if it exists, otherwise raises exception
As for why one might use one over the other… I have no idea. It seems as though you would get by using
:= for everything.
Things to do
Run an Io program from a file
This works the same way as Ruby (and I suppose, most things). Simply run the binary with the filename as the first argument, e.g.,
While we’re on the topic of working with files, I found a Vim plugin for Io called vim-io which adds support for syntax highlighting.
First you define a method by assigning
method(…) to a slot. Executing that method is as simple as sending the slot name to the object.
Io> Car drive := method("Vroom!" print) ==> method( "Vroom!" print ) Io> Car drive Vroom!==> Vroom!
You can also inspect the source code of a method with
getSlot("drive") code print.
Io is a straight-forward, minimalist language. I like the idea that there’s an extremely small amount of syntax to remember, and that the method names I’ve encountered so far do what you would expect them to do.
Chaining messages is interesting, though I think the lack of punctuation when chaining is likely to cause confusion with newcomers.
A couple of things Io has in common with Ruby (and most of the languages in the book), is that semicolons aren’t obligatory, and returns are implicit. These are small details that I’ve really come to appreciate.