Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Thirty Minutes
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Tue Sep 11, 2007 5:13 pm   Post subject: Thirty Minutes

Your first five minutes with Io

I'm going to guess that you're running Windows. If so, please find a Windows build of the Io environment here.

Unzip this to C:\usr\local.

You can now run it from a Command Prompt (Start -> Run -> "cmd") by executing the "io" binary in the aforementioned directory.

This gives you an interactive interpreter where you can type code and see immediate results.

Minutes six through ten: dipping your toes in the water

Hello, world!

code:
Io> "Hello, world!" println
Hello, world!

==> Hello, world!
Io>


Try using other strings. It's easy to do.

Try using numbers instead of strings.

Minutes eleven through fifteen: everything's an object

All of the strings... all of the numbers... those were objects. Objects respond to messages. We saw just such a message being sent with "println."

Naturally, there are other messages we can send to objects.

Let's determine the length of a string.

code:
"Hello, world!" size


But... what if we want to do this for a number? That isn't a string, and it doesn't respond to the "size" message.

code:
42 asString size


Yes, we can send a message to the result of another by simply tacking it on.

Exercise: modify the above such that it prints the size of 42 as a string.

Minutes sixteen through twenty: slots (no quarters necessary)

All objects have slots. Slots can hold any object. Those objects can be data, or they can be methods, which give the object a way to meaningfully repond to messages.

Let's add a simple slot to the object 42.

code:
42 setSlot("foo", 27)


We can later get at this value with:

code:
42 getSlot("foo")


But thre's a simpler, easier syntax for that.

code:
42 foo := 27


And:

code:
42 foo


Yep, 42 now knows how to respond to the message "foo."

A simple method might double the value of 42.

code:
42 double := method(self * 2)


code:
Io> 42 double
==> 84


Methods can take arguments.

code:
42 multipliedBy := method(x, self * x)


code:
Io> 42 multipliedBy(3)
==> 126


Exercise: implement a multipliedByAndAddingOne method that takes one argument.

Minutes twenty through twenty-five: inheritance

It's great that we can add slots to the object 42, but that's just one object. What if I want every number to respond to the "double" message?

Io uses what is known as "prototype-based inheritance." Don't worry about the term yet. It's more important to understand how it works.

When I send a message to an object, it checks to see if it can handle that message. If it can, that's great. If it can't, it checks to see if its prototype object can.

As it happens, 42's prototype is the object Number. So let's set a slot for Number.

code:
Number double := method(self * 2)


Now we can send that message to 42.

code:
Io> 42 double
==> 84


Oh, and wait...

code:
Io> 5 double
==> 10


Number is also the prototype for the object 5.

Minutes twenty-six through thirty: more than one prototype

In the previous five minutes, we dealt with the idea that objects have a prototype. If we send a message that an object can't handle, it'll send that message to its prototype.

We saw that 42 and 5 both have the prototype Number, so we added the "double" method to Number, such that any number can use it.

Now let's consider that Number has a prototype. In this case, that prototype is the base Object object. But we can also add another prototype to Number. Granted, number will first check Object for any messages it can't respond to, but we can have it check another object as well.

code:
Io> 42 double := method(self * 2)
==> method(
    self * 2
)
Io> Number appendProto(42)
==> 0
Io> 5 double
==> 10


In this case, 5 does not know how to handle the double message. So it sends the message to Number. Number does not know how to handle that message either. Number checks with Object, which doesn't have a clue, but then Number tries another prototype...

It tries the object 42, which does indeed know how to handle "double." The "self" object still refers to 5, so multiplying it by two yields...

10.

Smile

Congratulations

You've now seen thirty very gentle minutes worth of Io, and you've covered several weeks worth of concepts in a Java/C++ course.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: