Simplicity is the essence of happiness.
- Cedric Bledsoe
Io is a dyanmic prototype-based programming language. The ideas in Io are mostly inspired by Smalltalk[1] (all values are objects), Self[2] (prototype-based), NewtonScript[3] (differential inheritance), Act1[4] (actors and futures for concurrency), LISP[5] (code is a runtime inspectable / modifiable tree) and Lua[6] (small, embeddable).
Why Another Language?
To explore the idea that conceptual simplification leads to greater flexibility and power.
Goals
Io’s goal is to be a language that is:
•easy to use
•conceptually simple and consistent
•easily embedded
•easily extended
•useful
•fast enough
•multi-platform
•out-of-the-box ready for desktop, server and embedded scripting applications
Guidelines
Bindings Are Good
Many language communities view code outside the language as something to be avoided. Io embraces the idea of using C bindings for performance sensitive features (graphics, sound, encryption, array processing, etc) while maintaining multi-platform support by encouraging the use of platform independent or multi-platform C libraries (OpenGL, PortAudio, etc).
In order to provide a common base on which to build applications, the Io project also maintains official bindings for networking, graphics, sound, etc. that are included in the full distribution.
Objects are Good
When possible, bindings should provide an object oriented interface instead of mimicking low-level C APIs. Also, concrete design is favored over the abstract. Dozens of classes should not be required to do a simple operations.
Io distributions are available at:
To build, from the top folder, run:
make
Binaries will be placed in the _build/binaries subfolder. To install:
sudo make install
or, if you’d like the install to simply link to your development folder:
sudo make linkInstall
and to run the unit tests:
make test
Io builds two executables and places them in the binaries folder. They are:
io_static
io
The io_static executable contains the vm with a minimal set of primitives all statically linked into the executable. The io executable contains just enough to load the iovm dynamically linked library and is able to dynamically load io addons when they are referenced.
An example of running a script:
io samples/HelloWorld.io
There is no main() function or object that gets executed first in Io. Scripts are executed when compiled.
Running:
./_build/binaries/io
Or, if Io is installed, running:
io
will open the Io interpreter prompt.
You can evaluate code by entering it directly. Example:
Io> "Hello world!" println
==> Hello world!
Expressions are evaluated in the context of the Lobby:
Io> print
[printout of lobby contents]
Inspecting objects
You can get a list of the slots of an object like this:
Io> someObject slotNames
To show them in sorted order:
Io> someObject slotNames sort
doFile and doString
A script can be run from the interactive mode using the doFile method:
doFile(“scriptName.io”)
The evaluation context of doFile is the receiver, which in this case would be the lobby. To evaluate the script in the context of some other object, simply send the doFile message to it:
someObject doFile(“scriptName.io”)
The doString method can be used to evaluate a string:
Io> doString(“1+1”)
==> 2
And to evaluate a string in the context of a particular object:
someObject doString(“1 + 1”)
Command Line Arguments
Example of printing out command line arguments:
args foreach(k, v, write("'", v, "'\n"))
launchPath
The Lobby "launchPath" slot is set to the location on the initial source file that is executed.
Less is more.
- Ludwig Mies van der Rohe
Io has no keywords or statements. Everything is an expression composed entirely of messages, each of which is a runtime accessible object. The informal BNF description:
exp
message
arguments
symbol
terminator
|
::= { message | terminator }
::= symbol [arguments]
::= “(“ [exp [ { “,” exp } ]] “)”
::= identifier | number | string
::= “\n” | “;”
|
For performance reasons, String and Number literal messages have their results cached in their message objects.
Message arguments are passed as expressions and evaluated by the receiver. Selective evaluation of arguments can be used to implement control flow. Examples:
for(i, 1, 10, i println)
a := if(b == 0, c + 1, d)
In the above code, “for” and “if” are just normal messages, not special forms or keywords.
Likewise, dynamic evaluation can be used with enumeration without the need to wrap the expression in a block. Examples:
people select(person, person age < 30)
names := people map(person, person name)
There is also some syntax sugar for operators (including assignment), which are handled by an Io macro executed on the expression after it is compiled into a message tree. Some sample source code:
Account := Object clone
Account balance := 0
Account deposit := method(amount,
balance = balance + amount
)
account := Account clone
account deposit(10.00)
account balance println
Like Self[2], Io’s syntax does not distinguish between accessing a slot containing a method from one containing a variable.
An operator is just a message whose name contains no alphanumeric characters (other than ":", "_", '"' or ".") or is one of the following words: or, and, return. Example:
1 + 2
This just gets compiled into the normal message:
1 +(2)
Which is the form you can use if you need to do grouping:
1 +(2 * 4)
Standard operators follow C's precedence order, so:
1 + 2 * 3 + 4
Is parsed as:
1 +(2 *(3)) +(4)
User defined operators (that don't have a standard operator name) are performed left to right.
Io has two assignment messages, “:=” and “=”.
a := 1
which compiles to:
setSlot(“a”, 1)
which creates the slot in the current context. And:
a = 1
which compiles to:
updateSlot(“a”, 1)
which sets the slot if it is found in the lookup path or raises an exception otherwise. By overloading updateSlot and forward in the Locals prototype, self is made implicit in methods.
The following are valid number formats:
123
123.456
0.456
.456
123e-4
123e4
123.456e-7
123.456e2
Hex numbers are also supported (in any casing):
0x0
0x0F
0XeE
Strings can be defined surrounded by a single set of double quotes with escaped quotes (and other escape characters) within.
s := "this is a \"test\".\nThis is only a test."
Or for strings with non-escaped characters and/or spanning many lines, triple quotes can be used.
s := """this is a "test".
This is only a test."""
Comments of the //, /**/ and # style are supported. Examples:
a := b // add a comment to a line
/* comment out a group
a := 1
b := 2
*/
The "#" style is useful for unix scripts:
#!/usr/local/bin/io
That's it! You now know everything there is to know about Io's syntax. Control flow, objects, methods, exceptions are expressed with the syntax and semantics described above.
In all other languages we've considered [Fortran, Algol60, Lisp, APL, Cobol, Pascal], a program consists of passive data-objects on the one hand and the executable program that manipulates these passive objects on the other. Object-oriented programs replace this bipartite structure with a homogeneous one: they consist of a set of data systems, each of which is capable of operating on itself. - David Gelernter and Suresh J Jag
Io’s guiding design principle is simplicity and power through conceptual unification.
concept
prototypes
messages
blocks with assignable scope
|
unifies
objects, classes, namespaces, locals functions,
operators, calls, assignment, variable accesses
methods, closures, functions
|
In Io, everything is an object (including the locals storage of a block and the namespace itself) and all actions are messages (including assignment). Objects are composed of a list of key/value pairs called slots, and an internal list of objects from which it inherits called protos. A slot’s key is a symbol (a unique immutable sequence) and it’s value can be any type of object.
New objects are made by cloning existing ones. A clone is an empty object that has the parent in it’s list of protos. A new instance’s init slot will be activated which gives the object a chance to initialize itself. Like NewtonScript[3], slots in Io are create-on-write.
me := Person clone
To add an instance variable or method, simply set it:
myDog name := "rover"
myDog sit := method("I'm sitting\n" print)
When an object is cloned, its "init" slot will be called if it has one.
When an object receives a message it looks for a matching slot, if not found, the lookup continues depth first recursively in its protos. Lookup loops are detected (at runtime) and avoided. If the matching slot contains an activatable object, such as a Block or CFunction, it is activated, if it contains any other type of value it returns the value. Io has no globals and the root object in the Io namespace is called the Lobby.
Since there are no classes, there's no difference between a subclass and an instance. Here’s an example of creating a the equivalent of a subclass:
Io> Dog := Object clone
==> Object_0x4a7c0
The above code sets the Lobby slot "Dog" to a clone of the Object object. Notice it only contains a protos list contains a reference to Object. Dog is now essentially a subclass of Object. Instance variables and methods are inherited from the proto. If a slot is set, it creates a new slot in our object instead of changing the proto:
Io> Dog color := "red"
Io> Dog
==> Object_0x4a7c0:
color := "red"
Multiple Inheritance
You can add any number of protos to an object's protos list. When responding to a message, the lookup mechanism does a depth first search of the proto chain.
A method is an anonymous function which, when called, creates an object to store it's locals and sets the local’s proto pointer and it’s self slot to the target of the message. The Object method method() can be used to create methods. Example:
method((2 + 2) print)
An example of using a method in an object:
Dog := Object clone
Dog bark := method("woof!" print)
The above code creates a new "subclass" of object named Dog and adds a bark slot containing a block that prints "woof!". Example of calling this method:
Dog bark
The default return value of a block is the result of the last expression.
Arguments
Methods can also be defined to take arguments. Example:
add := method(a, b, a + b)
The general form is:
method(<arg name 0>, <arg name 1>, ..., <do message>)
A block is the same as a method except it is lexically scoped. That is, variable lookups continue in the context of where the block was created instead of the target of the message which activated the block. A block can be created using the Object method block(). Example of creating a block:
b := block(a, a + b)
Blocks vs. Methods
This is sometimes a source of confusion so it's worth explaining in detail. Both methods and blocks create an object to hold their locals when they are called. The difference is what the "proto" and "self" slots of that locals object are set to. In a method, those slots are set to the target of the message. In a block, they're set to the locals object where the block was created. So a failed variable lookup in a block's locals continue in the locals where it was created. And a failed variable lookup in a method's locals continue in the object to which the message that activated it was sent.
Call and Self Slots
When a locals object is created, it’s self slot is set (to the target of the message, in the case of a method, or to the creation context, in the case of a block) and it’s call slot is set to an object containing the following slots:
slot
sender
message
activated
slotContext
target
|
references
locals object of caller
message used to call this method/block
the activated method/block
context in which slot was found
current object
|
Variable Arguments
The “call message” slot in locals can be used to access the unevaluated argument messages. Example of implementing if() within Io:
if := method(
(call sender doMessage(call message argAt(0))) ifTrue(
call sender doMessage(call message argAt(1))) ifFalse(
call sender doMessage(call message argAt(2)))
)
myif(foo == bar, write("true\n"), write("false\n"))
The doMessage() method evaluates the argument in the context of the receiver.
A shorter way to express this is to use the evalArgAt() method on the call object:
if := method(
call evalArgAt(0) ifTrue(
call evalArgAt(1)) ifFalse(
call evalArgAt(2))
)
myif(foo == bar, write("true\n"), write("false\n"))
If an object doesn't respond to a message, it will invoke its "forward" method if it has one. Here’s an example of how to print the information related lookup that failed:
MyObject forward := method(
write("sender = ", call sender, "\n")
write("message name = ", call message name, "\n")
args := call message argsEvaluatedIn(call sender)
args foreach(i, v, write("arg", i, " = ", v, "\n") )
)
Sends the current message to the receiver's proto with the context of self. Example:
A := Object clone
A m := method(write("in A\n"))
B := A clone
B m := method(write("in B\n"); resend)
B m
will print:
in B
in A
For sending other messages to the receiver's proto, super is used.
Sometimes it's necessary to send a message directly to a proto. Example:
Dog := Object clone
Dog bark := method(writeln("woof!"))
fido := Dog clone
fido bark := method(
writeln("ruf!")
super(bark)
)
Both resend and super are implemented in Io.
Using the following methods you can introspect the entire Io namespace. There are also methods for modifying any and all of these attributes at runtime.
slotNames
The slotNames method returns a list of the names of an object’s slots:
Io> Dog slotNames
==> list(“bark”)
protos
The protos method returns a list of the objects which an object inherits from:
Io> Dog protos
==> list(“Object”)
getSlot
The "getSlot" method can be used to get the value of a block in a slot without activating it:
myMethod := Dog getSlot("bark")
Above, we've set the locals object’s "myMethod" slot to the bark method. It's important to remember that if you then want use the myMethod without activating it, you'll need to use the getSlot method:
otherObject newMethod := getSlot("myMethod")
Here, the target of the getSlot method is the locals object.
code
The arguments and expressions of methods are open to introspection. A useful convenience method is “code”, which returns a string representation of the source code of the method in a normalized form.
Io> method(a, a * 2) code
==> “method(a, a *(2))”
Io has predefined singletons for true, false and nil. true and false are used for boolean truth values and nil is typically used to indicate an unset or missing or unavailable value.
The standard comparison operations (==, !=, >=, <=, >, <) return either the true or false.
Io> 1 < 2
==> true
if
The Lobby contains the condition and loop methods. A condition looks like:
if(<condition>, <do message>, <else do message>)
Example:
if(a == 10, "a is 10" print)
The else argument is optional. The condition is considered false if the condition expression evaluates to false or nil, and is considered true otherwise.
The result of the evaluated message is returned, so:
if(y < 10, x := y, x := 0)
is the same as:
x := if(y < 10, y, 0)
Conditions can also be used in this form (though not as efficiently):
if(y < 10) then(x := y) else(x := 2)
Else-if is supported:
if(y < 10) then(x := y) elseif(y == 11) then(x := 0) else(x := 2)
As well as Smalltalk style ifTrue, ifFalse, ifNil and ifNonNil methods:
(y < 10) ifTrue(x := y) ifFalse(x := 2)
Notice that the condition expression must have parenthesis surrounding it.
loop
The loop method can be used for “infinite” loops:
loop(“foo” println)
while
Like conditions, loops are just messages. while() takes the arguments:
while(<condition>, <do message>)
Example:
a := 1
while(a < 10,
a print
a = a + 1
)
for
for() takes the arguments:
for(<counter>, <start>, <end>, <do message>)
The start and end messages are only evaluated once, when the loop starts.
Example:
for(a, 0, 10,
a print
)
To reverse the order of the loop, just reverse the start and end values:
for(a, 10, 0, a print)
Note: the first value will be the first value of the loop variable and the last will be the last value on the final pass through the loop. So a loop of 1 to 10 will loop 10 times and a loop of 0 to 10 will loop 11 times.
Example of using a block in a loop:
test := method(v, v print)
for(i, 1, 10, test(i))
repeat
The Number repeat method is simpler and more efficient when a counter isn’t needed.
3 repeat(“foo” print)
==> foofoofoo
break and continue
The flow control operations break and continue are supported in loops. For example:
for(i, 1, 10,
if(i == 3, continue)
if(i == 7, break)
i print
)
Would print:
12456
return
Any part of a block can return immediately using the return method. Example:
Io> test := method(123 print; return "abc"; 456 print)
Io> test
123
==> abc
The Importer proto implements Io’s built-in auto importer feature. If you put each of your proto’s in their own file, and give the file the same name with and “.io” extension, the Importer will automatically import that file when the proto is first referenced. The Importer’s default search path is the current working directory, but can add search paths using it’s appendSearchPath() method.
Io uses coroutines (user level cooperative threads), instead of preemptive OS level threads to implement concurrency. This avoids the substantial costs (memory, system calls, locking, caching issues, etc) associated with native threads and allows Io to support a very high level of concurrency with thousands of active threads.
The Scheduler object is responsible for resuming coroutines that are yielding. The current scheduling system uses a simple first-in-first-out policy with no priorities.
An actor is an object with it's own thread (in our case, it’s own coroutine) which it uses to process it's queue of asynchronous messages. Any object in Io can be sent an asynchronous message by placing a @ or @@ before the message name. (think of the "a" in @ as standing for "asynchronous")
Example:
result := self foo // synchronous message
futureResult := self @foo // async message, immediately return a Future
self @@foo // async message, immediately returns nil
When an object receives an asynchronous message it puts the message in its queue and, if it doesn't already have one, starts a coroutine to process the messages in its queue. Queued messages are processed sequentially in a first-in-first-out order. Control can be yielded to other coroutines by calling "yield". Example:
obj1 := Object clone
obj1 test := method(for(n, 1, 3, n print; yield))
obj2 := obj1 clone
obj1 @@test; obj2 @@test
while(Scheduler activeActorCount > 1, yield)
This would print "112233".
Here's a more real world example:
HttpServer handleRequest := method(aSocket,
HttpRequestHandler clone @@handleRequest(aSocket)
)
An object will automatically yield between processing each of its asynchronous messages. The yield method only needs to be called if a yield is required during an asynchronous message execution.
It's also possible to pause and resume an object. See the concurrency methods of the Object primitive for details and related methods.
Io's futures are transparent. That is, when the result is ready, they become the result. If a message is sent to a future (besides the two methods it implements), it waits until it turns into the result before processing the message. Transparent futures are powerful because they allow programs minimize blocking while also freeing the programmer from managing the fine details of synchronization.
Auto Deadlock Detection
An advantage of using futures is that when a future requires a wait, it will check to see if pausing to wait for the result would cause a deadlock and if so, avoid the deadlock and raise an exception. It performs this check by traversing the list of connected futures.
The @ and @@ Operators
The @ or @@ before an asynchronous message is just a normal operator message. So:
self @test
Gets parsed as(and can be written as):
self @(test)
An exception can be raised by calling raise() on an exception proto.
exceptionProto raise(<description>)
There are three predefined children of the Exception proto: Error, Warning and Notification. Examples:
Exception raise("generic foo exception")
Warning raise("No defaults found, creating them")
Error raise("Not enough memory")
To catch an exception, the try() method of the Object proto is used. try() will catch any exceptions that occur within it and return the caught exception or nil if no exception is caught.
e := try(<doMessage>)
To catch a particular exception, the Exception catch() method can be used. Example:
e := try(
// ...
)
e catch(Exception,
writeln(e coroutine backtraceString)
)
The first argument to catch indicates which types of exceptions will be caught. catch() returns the exception if it doesn't match and nil if it does.
To re-raise an exception caught by try(), use the pass method. This is useful to pass the exception up to the next outer exception handler, usually after all catches failed to match the type of the current exception:
e := try(
// ...
)
e catch(Error,
// ...
) catch(Exception,
// ...
) pass
Custom exception types can be implemented by simply cloning an existing Exception type:
MyErrorType := Error clone
Primitives are objects built into Io whose methods are typically implemented in C and store some hidden data in their instances. For example, the Number primitive has a double precision floating point number as it's hidden data and it’s methods that do arithmetic operations are C functions. All Io primitives inherit from the Object prototype and are mutable. That is, their methods can be changed. The reference docs contain more info on primitives.
This document is not meant as a reference manual, but an overview of the base primitives and bindings is provided here to give the user a jump start and a feel for what is available and where to look in the reference documentation for further details.
The ? Operator
Sometimes it's desirable to conditionally call a method only if it exists (to avoid raising an exception). Example:
if(obj getSlot("foo"), obj foo)
Putting a "?" before a message has the same effect:
obj ?foo
A List is an array of references and supports all the standard array manipulation and enumeration methods. Examples:
Create an empty list:
a := List clone
Create a list of arbitrary objects using the list() method:
a := list(33, "a")
Append an item:
a append(“b”)
==> list(33, “a”, “b”)
Get the list size:
a size
==> 3
Get the item at a given index (List indexes begin at zero):
a at(1)
==> "a"
Note: List indexes begin at zero and nil is returned if the accessed index doesn’t exist.
Set the item at a given index:
a atPut(2, "foo")
==> list(33, "a", "foo", "b")
a atPut(6, "Fred")
==> Exception: index out of bounds
Remove an item at a given index:
a remove(“foo”)
==> list(33, "a", "b")
Inserting an item at a given index:
a atPut(2, "foo")
==> list(33, "a", "foo", "56")
foreach
The foreach, map and select methods can be used in three forms:
Io> a := list(65, 21, 122)
In the first form, the first argument is used as an index variable, the second as a value variable and the 3rd as the expression to evaluate for each value.
Io> a foreach(i, v, write(i, “:”, v, “, ”))
==> 0:65, 1:21, 2:122,
The second form removes the index argument:
Io> a foreach(v, v println)
==> 65
21
122
The third form removes the value argument and simply sends the expression as a message to each value:
Io> a foreach(println)
==> 65
21
122
map and select
Io's map and select (known as filter in some other languages) methods allow arbitrary expressions as the map/select predicates.
Io> numbers := list(1, 2, 3, 4, 5, 6)
Io> numbers select(isOdd)
==> list(1, 3, 5)
Io> numbers select(x, x isOdd)
==> list(1, 3, 5)
Io> numbers select(i, x, x isOdd)
==> list(1, 3, 5)
Io> numbers map(x, x*2)
==> list(2, 4, 6, 8, 10, 12)
Io> numbers map(i, x, x+i)
==> list(1, 3, 5, 7, 9, 11)
Io> numbers map(*3)
==> list(3, 6, 9, 12, 15, 18)
The map and select methods return new lists. To do the same operations in-place, you can use selectInPlace() and mapInPlace() methods.
In Io, an immutable Sequence is called a Symbol and a mutable Sequence is the equivalent of a Buffer or String. Literal strings(ones that appear in source code surrounded by quotes) are Symbols. Mutable operations cannot be performed on Symbols, but one can make mutable copy of a Symbol calling it’s asMutable method and then perform the mutation operations on the copy.
Common string operations
Getting the length of a string:
"abc" size
==> 3
Checking if a string contains a substring:
"apples" containsSeq("ppl")
==> true
Getting the character (byte) at position N:
"Kavi" at(1)
==> 97
Slicing:
"Kirikuro" slice(0, 2)
==> "Ki"
"Kirikuro" slice(-2) # NOT: slice(-2, 0)!
==> "ro"
Io> "Kirikuro" slice(0, -2)
# "Kiriku"
Stripping whitespace:
" abc " adMutable strip
==> "abc"
" abc " asMutable lstrip
==> "abc "
" abc " asMutable rstrip
==> " abc"
Converting to upper/lowercase:
"Kavi" asUppercase
==> "KAVI"
"Kavi" asLowercase
==> "kavi"
Splitting a string:
"the quick brown fox" split
==> list("the", "quick", "brown", "fox")
Splitting by others character is possible as well.
"a few good men" split("e")
==> list("a f", "w good m", "n")
Converting to number:
"13" asNumber
==> 13
"a13" asNumber
==> nil
String interpolation:
name := "Fred"
==> Fred
"My name is #{name}" interpolate
==> My name is Fred
Interpolate will eval anything with #{} as Io code in the local context. The code may include loops or anything else but needs to return an object that responds to asString.
A range is a container containing a start and an end point, and instructions on how to get from the start, to the end. Using Ranges is often convenient when creating large lists of sequential data as they can be easily converted to lists, or as a replacement for the for() method.
The Range protocol
Each object that can be used in Ranges needs to implement a "nextInSequence" method which takes a single optional argument (the number of items to skip in the sequence of objects), and return the next item after that skip value. The default skip value is 1. The skip value of 0 is undefined. An example:
Number nextInSequence := method(skipVal,
if(skipVal isNil, skipVal = 1)
self + skipVal
)
With this method on Number (it’s already there in the standard libraries), you can then use Numbers in Ranges, as demonstrated below:
1 to(5) foreach(v, v println)
The above will print 1 through 5, each on its own line.
Tthe methods openForAppending, openForReading, or openForUpdating are used for opening files. To erase an existing file before opening a new open, the remove method can be used. Example:
f := File with(“foo.txt)
f remove
f openForUpdating
f write(“hello world!”)
f close
Creating a directory object:
dir := Directory with(“/Users/steve/”)
Get a list of file objects for all the files in a directory:
files := dir files
==> list(File_0x820c40, File_0x820c40, ...)
Get a list of both the file and directory objects in a directory:
items := Directory items
==> list(Directory_0x8446b0, File_0x820c40, ...)
items at(4) name
==> DarkSide-0.0.1 # a directory name
Setting a Directory object to a certain directory and using it:
root := Directory clone setPath("c:/")
==> Directory_0x8637b8
root fileNames
==> list("AUTOEXEC.BAT", "boot.ini", "CONFIG.SYS", ...)
Testing for existance:
Directory clone setPath("q:/") exists
==> false
Getthing the current working directory:
Directory currentWorkingDirectory
==> “/cygdrive/c/lang/IoFull-Cygwin-2006-04-20”
Creating a new date instance:
d := Date clone
Setting it to the current date/time:
d now
Getting the date/time as a number, in seconds:
Date now asNumber
==> 1147198509.417114
Date now asNumber
==> 1147198512.33313
Getting individual parts of a Date object:
d := Date now
==> 2006-05-09 21:53:03 EST
d
==> 2006-05-09 21:53:03 EST
d year
==> 2006
d month
==> 5
d day
==> 9
d hour
==> 21
d minute
==> 53
d second
==> 3.747125
Find how long it takes to execute some code:
Date cpuSecondsToRun(100000 repeat(1+1))
==> 0.02
All of Io’s networking is done with asynchronous sockets underneath, but operations like reading and writing to a socket appear to be synchronous since the calling coroutine is unscheduled until the socket has completed the operation, or a timeout occurs. Note that you’ll need to first reference the associated addon in order to cause it to load before using it’s objects. In these examples, you’ll have to reference “Socket” to get the Socket addon to load first.
Creating a URL object:
url := URL with("http://example.com/")
Fetching an URL:
data := url fetch
Streaming a URL to a file:
url streamTo(File with("out.txt"))
A simple whois client:
whois := method(host,
socket := Socket clone setHostName("rs.internic.net") setPort(43)
socket connect streamWrite(host, "\n")
while(socket streamReadNextChunk, nil)
return socket readBuffer
)
A minimal web server:
WebRequest := Object clone do(
handleSocket := method(aSocket,
aSocket streamReadNextChunk
request := aSocket readBuffer betweenSeq("GET ", " HTTP")
f := File with(request)
if(f exists, f streamTo(aSocket), aSocket streamWrite("not found"))
aSocket close
)
)
WebServer := Server clone do(
setPort(8000)
handleSocket := method(aSocket,
WebRequest clone @handleSocket(aSocket)
)
)
WebServer start
Using the XML parser to find the links in a web page:
xml := URL with("http://www.yahoo.com/") fetch asXML
links := xml elementsWithName("a") map(attributes at("href"))
Io’s C code is written using object oriented style conventions where structures are treated as objects and functions as methods. Familiarity with these may help make the embedding APIs easier to understand.
Structures
Member names are words that begin with a lower case character with successive words each having their first character upper cased. Acronyms are capitalized. Structure names are words with their first character capitalized.
Example:
typdef struct
{
char *firstName;
char *lastName;
char *address;
} Person;
Functions
Function names begin with the name of structure they operate on followed by an underscore and the method name. Each structure has a new and free function.
Example:
List *List_new(void);
void List_free(List *self);
All methods (except new) have the structure (the "object") as the first argument the variable is named "self". Method names are in keyword format. That is, for each argument, the method name has a description followed by an underscore. The casing of the descriptions follow that of structure member names.
Examples:
int List_count(List *self); /* no argument */
void List_add_(List *self, void *item); /* one argument */
void Dictionary_key_value_(Dictionary *self, char *key, char *value);
File Names
Each structure has it's own separate .h and .c files. The names of the files are the same as the name of the structure. These files contain all the functions(methods) that operate on the given structure.