I suggest, that you first consider, how would you do that in JS. You'll need to wrap that in functions:<div><br></div><div>{</div><div>'fn_1' : (function () {</div><div> var x = 1;</div><div> return function () { return x; } }) (),</div>
<div>'fn_2' : (function () {</div><div><div> var x = 2;</div><div> return function () { return x; } }) ()</div><div>}</div><div><br></div><div>Now let's think, how this can be done in Parenscript?..</div>
<div><br></div><div>PS. But the most important question is: why do you need to create a single function, that closes over a "private" variable, as part of an object? Isn't it equivalent to just coding the value of the variable inside the function?</div>
<div><br></div><div>vsevolod<br>
<br><br><div class="gmail_quote">On Thu, Oct 13, 2011 at 10:05 AM, Canhua <span dir="ltr"><<a href="mailto:dreameration@gmail.com">dreameration@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
actually what I want to achieve is something like this:<br>
(create "fn_1" (let ((x))<br>
#'(lambda ()<br>
x))<br>
"fn_2" (let ((x))<br>
#'(lambda ()<br>
x)))<br>
and I expected these two "x" are lexical-scope separate and so<br>
independent from each other.<br>
However the compiled js code doesn't work as I expected.<br>
<div><div></div><div class="h5"><br>
<br>
On Thu, Oct 13, 2011 at 2:51 PM, Vsevolod Dyomkin <<a href="mailto:vseloved@gmail.com">vseloved@gmail.com</a>> wrote:<br>
> Hi<br>
> Actually the above code is correct.<br>
> You can also use:<br>
> - either<br>
> (let (x)<br>
> (create "fn" (lambda () x)))<br>
> - or<br>
> (create "x" nil<br>
> "fn" (lambda () x)))<br>
> depending on the JS semantics you want to get.<br>
> vsevolod<br>
><br>
><br>
> On Thu, Oct 13, 2011 at 8:40 AM, Canhua <<a href="mailto:dreameration@gmail.com">dreameration@gmail.com</a>> wrote:<br>
>><br>
>> hi, all, I found that<br>
>> (create "fn" (let ((x))<br>
>> (lambda () x)))<br>
>><br>
>> compiles to<br>
>> { 'fn' : (x = null, function () {<br>
>> return x;<br>
>> }) }<br>
>><br>
>> wherein the variable x may conflict with a variable with the same name<br>
>> outside this code.<br>
>> How may avoid this? How may I achieve "let over lambda" closure effect<br>
>> as in common lisp?<br>
>><br>
>> Thanks.<br>
>><br>
>> _______________________________________________<br>
>> parenscript-devel mailing list<br>
>> <a href="mailto:parenscript-devel@common-lisp.net">parenscript-devel@common-lisp.net</a><br>
>> <a href="http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel" target="_blank">http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel</a><br>
><br>
><br>
> _______________________________________________<br>
> parenscript-devel mailing list<br>
> <a href="mailto:parenscript-devel@common-lisp.net">parenscript-devel@common-lisp.net</a><br>
> <a href="http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel" target="_blank">http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel</a><br>
><br>
><br>
<br>
_______________________________________________<br>
parenscript-devel mailing list<br>
<a href="mailto:parenscript-devel@common-lisp.net">parenscript-devel@common-lisp.net</a><br>
<a href="http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel" target="_blank">http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel</a><br>
</div></div></blockquote></div><br></div></div>