How to Do Prototype Based Programming with Io

Learn to program using Io - OOP with prototypes

© Mark Alexander Bain

Nov 10, 2008
Io: prototype based rogramming, Mark Alexander Bain
How is it possible to use object oriented programming without classes? Easy - use prototypes and Io.

One of the interesting things about using classes in a programming language such as Io is that it doesn't actually use classes. Instead Io uses a concept known as prototyping, which makes Io a very easy programming language to understand.

A new user can be up and running in just a matter of minutes - even if they've never written any code before.

The Concept of Prototyping

The core to prototyping is cloning - a prototype object is created and then this is cloned to produce new objects. This concept can easily be demonstrated by looking at Io.

Downloading Io

Both Windows and Linux can be obtained from the Io download page at http://www.iolanguage.com/downloads, and the installation is simple:

  • Windows - download the binaries zip file to the C: drive, and then uncompress it - the result will be a directory named C:\usr\local\bin and it's here that the Io program lives
  • Linux - download the tarballs, uncompress and run:
    make vm
    sudo make install

On some versions of Linux a little extra work may be needed

cd <uncompressed folder>
sudo cp libiovmall.so /usr/lib/

Running Io

Io can by run in any of three ways:

  • calling Io directly from the command line, for example:
    $ io
    Io> writeln ("Hello World")
  • using Io to load a script file:
    io <my Io script file>
  • make an Io script file executable and then run that from the command line

An Io Script: Prototypes and Clones

The first line of any Io script should be the shebang line; this tells the system that the Io interpreter is to be used:

#!/usr/local/bin/io

The key thing that this script will do is to create a prototype (in this case a car), and it's interesting to note that the car (at this stage) is just a clone of Io's default object:

car := Object clone

At the moment car is just an blank object, however it is very easy to add properties and methods to the basic object:

car fuel_level := 0
car fuel_capacity := 10
car add_fuel := method(
gallons_in,
gap := fuel_capacity - fuel_level
if ( gallons_in > gap,gallons_in := gap )
writeln("Adding " , gallons_in," gallons")
fuel_level = fuel_level + gallons_in
)

To make another object the programmer can clone the original and then make use of it:

jeep := car clone
writeln( jeep fuel_level , " gallons")
jeep add_fuel(5)
writeln( jeep fuel_level , " gallons")
jeep add_fuel(6)
writeln( jeep fuel_level , " gallons")

If the script were to be run now then the output would be:

0 gallons
Adding 5 gallons
5 gallons
Adding 5 gallons
10 gallons

Expanding the Prototype

In this example a simple prototype (the car) was created and cloned (the jeep), however it is still possible to add further properties and methods to the prototype:

car do (
odometer_reading := 0
mpg := 30
fuelgauge := method(writeln("Fuel: ", fuel_level, " gallon"))
odometer := method (writeln ("Distance travelled: ", odometer_reading, " miles"))
travel := method (distance,
gallons_used := distance / mpg
if ( gallons_used > fuel_level,
gallons_used = fuel_level
distance = fuel_level * mpg
writeln ("Maximum distance ", distance, " miles")
)
odometer_reading = odometer_reading + distance
fuel_level = fuel_level - gallons_used
)
)

and these additions will passed on to any clones automatically:

jeep fuel_capacity = 12
jeep add_fuel(12)
jeep travel (30)
jeep fuelgauge
jeep odometer
jeep travel (1000)
jeep fuelgauge
jeep odometer

with an output of:

Adding 12 gallons
Fuel: 11 gallon
Distance travelled: 30 miles
Maximum distance 330 miles
Fuel: 0 gallon
Distance travelled: 360 miles

And, of course, further clones can be created:

smart := car clone
smart mpg = 70
smart add_fuel(12)
smart travel (70)
smart fuelgauge
smart odometer
smart travel (1000)
smart fuelgauge
smart odometer
Adding 10 gallons
Fuel: 9 gallon
Distance travelled: 70 miles
Maximum distance 630 miles
Fuel: 0 gallon
Distance travelled: 700 miles

Conclusion

Io provides an excellent introduction into prototype-based programming and provides the programmer with:

  • the ability to create a prototype
  • the ability to create clones from the prototype
  • behaviour reuse -allowing the clone to automatically pick up properties and methods from the prototype

It is all so simple that even a complete novice can program with Io with complete ease.


The copyright of the article How to Do Prototype Based Programming with Io in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish How to Do Prototype Based Programming with Io in print or online must be granted by the author in writing.


Io: prototype based rogramming, Mark Alexander Bain
Io may be run from the command line or scripts, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Dec 27, 2008 6:04 PM
Guest :
After reading your post, now I have a fair understanding of what's prototype based programming about. Very well explained. Thanks!

I'll stay tunned for more Io articles!

1 Comment: