1

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)
iLemming
  • 1,223
  • 9
  • 14
  • 1
    Does this answer your question? [Void Function Error](https://emacs.stackexchange.com/questions/44643/void-function-error) – Drew Jan 02 '20 at 15:25

1 Answers1

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)
phils
  • 48,657
  • 3
  • 76
  • 115
  • 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 02 '20 at 05:45