Sinister Scheme Sample Perplexes Python Porter
started 1/24/2002; 6:00:06 AM - last post 1/24/2002; 2:05:07 PM
|
|
Ehud Lamm - Sinister Scheme Sample Perplexes Python Porter
1/24/2002; 6:00:06 AM (reads: 1813, responses: 4)
|
|
|
Noel Welsh - Re: Sinister Scheme Sample Perplexes Python Porter
1/24/2002; 6:53:36 AM (reads: 949, responses: 0)
|
|
I could hear his propellor beanie whirring from here ;)
|
|
Sjoerd Visscher - Re: Sinister Scheme Sample Perplexes Python Porter
1/24/2002; 9:07:44 AM (reads: 935, responses: 0)
|
|
I had to try this in Javascript. And the ruby version inspired me to this solution. (The eval and the quotes around the variablename are required because you can't get to variables in other scopes.)
function address(str) {
return "_dummy_={"+
"get:function() {return "+str+";},"+
"set:function(_"+str+") {return "+str+"=_"+str+";}"+
"}";
}
var x=1;
var px=eval(address("x"));
alert(px.get());
px.set(2);
alert(x);
|
|
Wilhelm Fitzpatrick - Re: Sinister Scheme Sample Perplexes Python Porter
1/24/2002; 1:00:49 PM (reads: 911, responses: 0)
|
|
What would happen in the Javascript example if px were passed into a different context that had a different binding for x? Wouldn't it then affect THAT x? Or am I missing something here?
|
|
Dan Shappir - Re: Sinister Scheme Sample Perplexes Python Porter
1/24/2002; 2:05:07 PM (reads: 891, responses: 0)
|
|
No, it would effect the correct x. This is because a context (scope) in JavaScript defines a proper closure. The getter and setter functions manipulate the x in the same scope in which they were defined. That is the reason for the whole eval thing: to actually create the functions in the correct context.
|
|
|
|