Last active
December 15, 2015 20:00
-
-
Save smosher/5315773 to your computer and use it in GitHub Desktop.
DSL for List literals using List [ ... ] as a List-scoped constructor
This file contains 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
/* Unsafe version: | |
List squareBrackets := method( | |
call message arguments clone map(x, | |
call sender doMessage(x) | |
) | |
) | |
*/ | |
// Safe version: | |
List squareBrackets := method( | |
env := call sender clone | |
call message arguments clone map(x, | |
env doMessage(x) | |
) | |
) | |
/* | |
// Note: the unsafe version allows the following: | |
Io> q | |
Exception: Object does not respond to 'q' | |
--------- | |
Object q Command Line 1 | |
Io> List [1,2,3,(q := 10)] | |
==> list(1, 2, 3, 10) | |
Io> q // <-- q leaked out | |
==> 10 | |
// But with the safe version: | |
Io> List [1,2,3,(z := 10), (z + 4)] | |
==> list(1, 2, 3, 10, 14) // check it out | |
Io> z // <-- z was evaluated in a clone environment, no leaks | |
Exception: Object does not respond to 'z' | |
--------- | |
Object z Command Line 1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment