Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created December 8, 2008 18:27
Show Gist options
  • Save mkhl/33551 to your computer and use it in GitHub Desktop.
Save mkhl/33551 to your computer and use it in GitHub Desktop.
Constants in Io
Constant := Object clone do (
with := method(msg,
self msg := msg
self activate := method(call message setName(msg asString); msg)
)
)
// Lets try it out:
// Make PI a regular number
PI := 3.1415926
// Make b a method that uses it
b := method(PI+PI)
writeln(b) // -> 6.283185
// Inspect bs "source" code
writeln(getSlot("b") code) // -> block(PI +(PI))
// Make PI a constant
PI := Constant with(3.1415926)
writeln(b) // -> 6.283185
writeln(getSlot("b") code) // -> block(3.141593 +(3.141593))
// Still works but when we look at the code of b we see that
// the PIs have been replaced with PIs value!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment