seven-languages-in-seven-weeks/slides/io.org
2015-06-07 22:37:57 -04:00

6.6 KiB

Seven Languages in Seven Weeks

Introduction

Introduction

Io   BMCOL

Created
2002
Author
Steve Dekorte

An embeddable prototype language with a simple syntax and strong concurrency model.

Ferris Bueller   BMCOL

/github/seven-languages-in-seven-weeks/media/commit/fc8fa55b1fc22ea246019f935c35f5f79df62d4c/slides/bueller.jpg

Day 1

Day 1: Skipping School, Hanging Out

  • Prototypes
  • Sending messages
  • Objects, slots, and types
  • Methods
  • Collections
  • Singletons

Objects, Prototypes, and Inheritance

  Vehicle := Object clone
  Vehicle description := "Something to take you far away"
  Car := Vehicle clone
  Car description // "Something to take you far away"
  ferrari := Car clone
  • Objects are just containers of slots. If the slot isn't there, Io calls the parent.
  • ferrari has no type slot. By convention, types in Io begin with uppercase letters.

Methods

  Car drive := method("Vroom" println)
  • A method is an object.
  • Since a method is an object, we can assign it to a slot.
  • If a slot has a method, invoking the slot invokes the method.

Lists and Maps

  list(1, 2, 3, 4) sum // 10

  elvis := Map clone
  elvis atPut("home", "Graceland")
  elvis at("home") // "Graceland"
  • A list is an ordered collection of objects of any type.
  • A hash is a lot like an Io object in structure where the keys are slots that are tied to values.

true, false, nil, and singletons

  4 < 5          // true
  4 <= 3         // false
  true and false // false
  true or false  // true
  true and 6     // true
  true and 0     // true

  true clone     // true
  false clone    // false
  nil clone      // nil

true, false, and nil are singletons.

Singletons

  Highlander := Object clone
  Highlander clone := Highlander

We've simply redefined the clone method to return Highlander, rather than letting Io forward requests up the tree, eventually getting to Object.

Exercises

EXERCISES

Day 2

Day 2: The Sausage King

  • Loops & Conditionals
  • Operators
  • Messages
  • Reflection

Loops

Infinite loop

  loop("getting dizzy..." println)

While loop

  i := 1
  while(i <= 11, i println; i = i + 1); "This one goes up to 11" println

For loop

  for(i, 1, 11, i println); "This one goes up to 11" println

Conditionals

The if control structure is implemented as a function with the form if(condition, true code, false code). The function will execute true code if condition is true; otherwise, it will execute false code.

Operators

0 ? @ @@
1 **
2 % * /
3 + -
4 << >>
5 < <= > >=
6 != ==
7 &
8 ^
9 \vert
10 && and
11 or
12 ..
13 %= &= *= += -= /= <<= >>= ^= \vert=
14 return

Assign Operators

::= newSlot
:= setSlot
= updateSlot

Add an Operator

  OperatorTable addOperator("xor", 11)

  true xor := method(bool, if(bool, false, true))
  false xor := method(bool, if(bool, true, false))

Messages

A message has three components: the sender, the target, and the arguments.

The call method gives you access to the meta information about any message.

Reflection

  Object ancestors := method(
          prototype := self proto  
          if(prototype != Object,
          writeln("Slots of ", prototype type, "\n---------------")
          prototype slotNames foreach(slotName, writeln(slotName))
          writeln
          prototype ancestors))


  Animal := Object clone
  Animal speak := method(
              "ambiguous animal noise" println)

  Duck := Animal clone
  Duck speak := method(
              "quack" println)

  Duck walk := method(
              "waddle" println)

  disco := Duck clone
  disco ancestors

Exercises

EXERCISES

Day 3

Day 3: The Parade and Other Strange Places

  • DSLs
  • Metaprogramming
  • Concurrency

Domain-Specific Languages

  OperatorTable addAssignOperator(":", "atPutNumber")
  curlyBrackets := method(
    r := Map clone
    call message arguments foreach(arg,
         r doMessage(arg)
         )
    r
  )
  Map atPutNumber := method(
    self atPut(
         call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\""),
         call evalArgAt(1))
  )
  s := File with("phonebook.txt") openForReading contents
  phoneNumbers := doString(s)
  phoneNumbers keys   println
  phoneNumbers values println

Io's method_missing

  Builder := Object clone
  Builder forward := method(
    writeln("<", call message name, ">")
    call message arguments foreach(
          arg, 
          content := self doMessage(arg); 
          if(content type == "Sequence", writeln(content)))
    writeln("</", call message name, ">"))
  Builder  ul(
          li("Io"), 
          li("Lua"), 
          li("JavaScript"))

Concurrency

  • Coroutines
  • Actors
  • Futures

Exercises

EXERCISES

Wrapping Up

Wrapping Up: Strengths

  • Footprint
  • Simplicity
  • Flexibility
  • Concurrency

Wrapping Up: Weaknesses

  • Syntax
  • Community
  • Performance

Final Thoughts

Like Lisp, Io has a strong overriding philosophy of simplicity and flexibility.