0

I'm having difficulty to call a function passed as an argument. Why the following snippet doesn't work, and how can I make it work?

lexical-binding is set to t

(defun on-success (data)
  (print "This works!")
  (print data))

(defun send-req (url callback)
  (request url
   :success (function*
             (lambda (&key data &allow-other-keys)
               (print (type-of callback)    ; <- this prints 'symbol', without lexical binding it would be nil
               (funcall 'callback data)))))

(defun foo ()
  (send-req
   "https://google.com"
   'on-success))

(foo)
1
1

Don't quote callback here:

(funcall 'callback data)

Quoted, you've said to call the function named "callback" (i.e. using the function slot of the callback symbol).

What you want to do is call the function in the value of the callback argument:

(funcall callback data)
1
  • Yup, you are right. Thank you! Also while testing I learned that if funcall doesn't apply the correct number of args, it just won't work at all either.
    – iLemming
    Jan 2 '20 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.