help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to use a variable from outer scope in the success function of a


From: Pascal J. Bourguignon
Subject: Re: How to use a variable from outer scope in the success function of a request call?
Date: Sat, 29 Aug 2015 12:31:11 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Iñigo Serna <address@hidden> writes:

> Hi,
>
> Pascal J. Bourguignon writes:
>>> ; I can't use a variable inside the success lambda function of a web 
>>> request call.
>>> ; As this is difficult to explain I'll show some things I've tried.
>>
>> Add:
>>   (setf lexical-binding t)
>> before fn1, and:
>>   ;; -*- mode:emacs-lis;lexical-binding:t -*-  
>> as the first line of all your .el files.
>
> Thanks a lot Pascal, that makes fn1 `work'!
> And to learn more myself, do you have any hint why `fn3' or `fn4' don't work 
> and how to make
> it work?
>
> Thanks,
> Iñigo Serna
>

Use pp macroexpand to see what your macro calls expand to:

(pp (macroexpand '(my-request2 "https://api.stackexchange.com/2.1/questions";
                   #'(lambda (data &optional d)
                       (let* ((item (elt (assoc-default 'items data) 0))
                              (title (assoc-default 'title item))
                              (tags (assoc-default 'tags item)))
                         (message "PARAMS: %S" d)
                         (message "=> %s %S" title tags)
                         (message "LAST: %S" (nth 2 d))))
                   '("OOOO" "AAAA" c))))

(request "https://api.stackexchange.com/2.1/questions"; :params
         '((order . "desc")
           (sort . "activity")
           (site . "stackoverflow"))
                                                       :parser 'json-read 
:success
         (function*
          (lambda
              (&key data &allow-other-keys)
           (funcall
            #'(lambda
                  (data &optional d)
                (let*
                    ((item
                       (elt
                        (assoc-default 'items data)
                        0))
                     (title
                         (assoc-default 'title item))
                     (tags
                       (assoc-default 'tags item)))
                  (message "PARAMS: %S" d)
                  (message "=> %s %S" title tags)
                  (message "LAST: %S"
                           (nth 2 d))))
            data
            '("OOOO" "AAAA" c)))))

>From this, you can see that the parameter c of fn3 is never used
(you just pass the symbol c to your callback).


Similarly, for fn4:

(pp (macroexpand '(my-request2 "https://api.stackexchange.com/2.1/questions";
                   #'(lambda (data &optional d)
                       (let* ((item (elt (assoc-default 'items data) 0))
                              (title (assoc-default 'title item))
                              (tags (assoc-default 'tags item)))
                         (message "PARAMS: %S" d)
                         (message "=> %s %S" title tags)
                         (message "LAST: %S" (nth 2 d))))
                   (list "OOOO" "AAAA" c))))

(request "https://api.stackexchange.com/2.1/questions"; :params
         '((order . "desc")
           (sort . "activity")
           (site . "stackoverflow"))
                                                       :parser 'json-read 
:success
         (function*
          (lambda
              (&key data &allow-other-keys)
           (funcall
            #'(lambda
                  (data &optional d)
                (let*
                    ((item
                       (elt
                        (assoc-default 'items data)
                        0))
                     (title
                         (assoc-default 'title item))
                     (tags
                       (assoc-default 'tags item)))
                  (message "PARAMS: %S" d)
                  (message "=> %s %S" title tags)
                  (message "LAST: %S"
                           (nth 2 d))))
            data
            (list "OOOO" "AAAA" c)))))

You can see that your macro expands to a call to request with as
argument an anonymous function that refers a free variable named c.

However, without lexical-binding, variables are dynamic.  

    lexical = space = where
    dynamic = time  = when

Therefore WHEN this anonymous function is called, it may happen that no
variable named c exist anymore. Or worse, than another variable named c
THEN exist that is not the one that existed when your function fn4 was
executing!



In the context of Common Lisp, here is an explaination of
lexical/dynamic and global/local
https://groups.google.com/forum/#!msg/comp.lang.lisp/4VyopdWcFI4/1sDQU-3H8VgJ

Google also about the FUNARG problem.

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]