[parenscript-devel] Bug: let within an outer let's init-form
Andy Peterson
andy.arvid at gmail.com
Thu Nov 29 15:08:38 UTC 2012
Vladmir,
Thanks for the detailed response. I can see you are right. Now I am
totally confused why I got the error a month ago in google chrome as I can
not recreate it. I swear I tested my original example. I remember writing
an function a and b and testing. On top of that, the original macro I wrote
now seems to work within a let.
Anyway, this is the original macro for a "functional-pseudo-chain":
(defpsmacro fpchain (object &rest chains)
"generate a pseudo-chain of method calls of given object
and return the object. for use of js prototypes that do not
allow chaining like j-query."
(ps-once-only (object)
(append '(progn)
(mapcar (lambda (x) `(chain ,object ,x))
chains)
`(,object))))
I use this primarily for the html5 canvas context object which requires a
lot of method calls in a row but does not allow chaining.
Similar to ps:chain except for the fact it does not chain properties
(fpchain foo bar (baz)) does not work.
thanks again,
andy peterson
On Thu, Nov 29, 2012 at 12:31 AM, Vladimir Sedach <vsedach at gmail.com> wrote:
> Believe it or not, that's actually valid JavaScript code:
>
> PS> (ps (let ((x (let ((y 12))
> (+ 1 2)
> y)))
> (1+ x)))
> "(function () {
> var y;
> var x = (y = 12, (1 + 2, y));
> return x + 1;
> })();"
> PS> (cl-js:run-js *)
> 13
>
> In your example, a() and b() were expressions, so the inner let
> generated a sequence of expressions by using the comma operator. If
> for example you replace b() by a statement:
>
> PS> (ps (let ((x (let ((y 12))
> (dolist (x '(1 2 3))
> (* x x))
> y)))
> (1+ x)))
> "(function () {
> var y;
> var x4 = (y = 12, ((function () {
> for (var x = null, _js_arrvar6 = [1, 2, 3], _js_idx5 = 0;
> _js_idx5 < _js_arrvar6.length; _js_idx5 += 1) {
> x = _js_arrvar6[_js_idx5];
> x * x;
> };
> })(), y));
> return x4 + 1;
> })();"
> PS> (cl-js:run-js *)
> 13
>
> This is ugly, and I would love examples of how to generate better code
> for nested LETs.
>
> Happy hacking,
> Vladimir
>
>
> On Wed, Oct 24, 2012 at 5:50 AM, Andy Peterson <andy.arvid at gmail.com>
> wrote:
> > If you use a let within the init-form of an outer let, the result is
> invalid
> > javascript code.
> >
> > Here is a simplified example:
> >
> > (ps (let ((x (let ((y (a)))
> > (b)
> > y)))
> > (1+ x)))
> >
> > ==>
> >
> > "(function () {
> > var y;
> > var x = (y = a(), (b(), y));
> > return x + 1;
> > })();"
> >
> > Not that I would normally write such code, but the inner "let" was
> generated
> > by a macro.
> > And many macros use "let" with gensyms. In my case, the macro used
> > "ps-once-only".
> >
> > Andy Peterson
> >
> > _______________________________________________
> > parenscript-devel mailing list
> > parenscript-devel at common-lisp.net
> > http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
> >
>
> _______________________________________________
> parenscript-devel mailing list
> parenscript-devel at common-lisp.net
> http://lists.common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/parenscript-devel/attachments/20121129/bb0f2290/attachment.html>
More information about the parenscript-devel
mailing list