Created
December 8, 2008 18:27
-
-
Save mkhl/33551 to your computer and use it in GitHub Desktop.
Constants in Io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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