Skip to content

Instantly share code, notes, and snippets.

@smosher
Last active December 15, 2015 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smosher/5315773 to your computer and use it in GitHub Desktop.
Save smosher/5315773 to your computer and use it in GitHub Desktop.
DSL for List literals using List [ ... ] as a List-scoped constructor
/* 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