Io (programming language)

From Bauman National Library
This page was last modified on 21 May 2016, at 23:45.
Io
Io lang.png
Paradigm Object-oriented, prototype
Designed by Steve Dekorte
First appeared 2002
Typing discipline Dynamic
License BSD
Website http://iolanguage.org/
Influenced by
Smalltalk, NewtonScript, Self, Lua, Lisp, Act1

Io - pure script object-oriented programming language with dynamic type system known for its simplicity. Developed by Steve Dekorte in 2002. Realization of the language is cross-platform and open (BSD-licensed).

Program code in Io is translated and run by compact virtual machine. Packages of Io could be easily found on it's Web-site. There is also a runtime interpreter.

History

Io language was created in 2002, when its autor decided to write simple and minimalistic language. There is noted infuence of Smalltallk (it's principle 'everythin is an object), Self and NewtonScript (prototype object system), Lisp (data and commands are equal in programs) and also Lua and Act1. Pixar also created their own modification of Io - It language.

Features

  • Minimalistic syntax
  • Everything is an object
  • Objects exchange messages (Smalltalk-style)
  • Concurrency, based on actors
  • No classes, uses prototype system
  • Programs are just data trees (like in Lisp)
  • Big metaprogramming potential
  • Dynamic type system

Syntax

  • Io programs don't need special function main() or any other object where a program should start. So programs in Io are scripts.
  • Io doesn't have reserved keywords and operators. Code of any program is written only by means of equations, compiled from messages, each of which is an object that is available (and changable) at any time during the program execution.
  • In order to provide logical control structures in the language Io has special singletons true or false', and also a special Singleton nil, indicating the absence of a value.
  • Logical comparison operations are performed using the methods ==, !=, >, <, <=, >=, which return true or false. Also, there is a method compare(), which returns a value of -1 (less) 0 (equal) or 1 (greater than) and used for the implementation of specialized methods of comparison values.
  • Object in Io is a special container, consisted of slots. Slot is a storage unit of something (object, method). Io has 3 different assignment operations:
    =     Update a slot. If slot is gone or never existed an exception is raised
    :=    Set a slot. Creates a new slot or updates an old one with new value
    ::=   Create a slot. Creates a new slot and sets a value
  Messages are sent to objects and written postfix style separated by space: Object message
  Creation of a slot: SomeObject someSlot := "hello world"
  Creation of a method: SomeObject sum := method(a, b, a + b)
  • Strings could be put in single quotes " or triple """, alowding many-lines strings. Escaping characters provided by backslash \. Non-typed symbols are the same as in C: \n \t and so on.
  • Language uses 3 types of comments:
  /* comment */ - many-lined
  // comment - one-lined
  #!/usr/bin/io - one-lined, using for declarating interpreter path
  • Io alows full introspection:
  Dog := Object clone
  Dog bark := method(writeln("woof!"))

Get list of methods:

  Io> Dog slotNames
  ==> list("bark")

Get list of prototypes:

  Io> Dog protos
  ==> list("Object")

Get normalized text form of code:

  Io> method(a, a * 2) code
  ==> "method(a, a *(2))"
  • Io uses user-level threads for concurrency. Any object runs it's procedures in own thread. It minimizes time for scheduling and alowes normal work of thouthands of threads. Any object can send and receive messages asynchronously. Reveived messages are keeped in queues. If messages is not received yet, thread is blocked.
  HttpServer handleRequest := method(aSocket,
    HttpRequestHandler clone asyncSend(handleRequest(aSocket))
  )
  • System can monitor deadlocks. If it detects one, an exception is raised.
  • Excepttions
  Generation:  Exception raise("generic foo exception")
  Handling: 
  e := try(
    // ...
  ) 
  e catch(Exception,
    writeln(e coroutine backTraceString)
  )
  • Primitives
  obj ?foo  -  operator ? alowes to use method only if it exists
  List - list, could be also created by list()
  map, select - functions for process lists
  File - file. f := File with("foo.txt")
  Directory - d := Directory with ("/etc/")

Examples

print a message to console

    "Hello world" print

Conditional operators

    if(a == 1) then(
      writeln("a is one")
    ) else(
      writeln("a is not one")
    )

    if(a == 1, writeln("a is one"), writeln("a is not one"))

    x := if( sum < 100, sum, 100 )

Cycle

    for( <counter>, <start_value>, <end_value>, [<step>,] <message> )

    while( <condition>, <message> )

OOP

    Mushroom := Object clone
    Mushroom isPoison := false
    Mushroom whenEaten := method(person,
      if(self isPoison == true, 
        person kill
      ) 
    )

    InfectedMushroom := Mushroom clone
    InfectedMushroom isPoison := true 

    Man := Object clone
    Man state := "Living"
    Man eat := method(food,
       food whenEaten(self)
    )

    Man kill := method(
      self state := "Dead"
    )

References