3

... when a symbol referencing a function is actually used?

Documentation states:

Like ‘add-function’ but for the function named SYMBOL. Contrary to ‘add-function’, this will properly handle the cases where SYMBOL is defined as a macro, alias, command, ...

I usually see the following around:

(advice-add 'some-function :after #'some-other-function)

My question is, wouldn't be the case to use #'some-function in that case?

Of course I tried, and it works, and in fact with the sharp quote, Emacs is able to throw a warning if some-function is not defined when I byte-compile the code.

I even tried advising aliased functions (with defalias) and saw no difference.

So why the common advice is not to use the sharp quote for SYMBOL too?

5
  • If advice-add behaves like defadvice then there's no need for some-function to be defined when you byte-compile the code. The function will be advised if and when it becomes defined. I guess if you want byte-compiler warnings being thrown in the case where the function is not yet defined, then #' would be useful.
    – phils
    Nov 9, 2017 at 19:57
  • @phils That's the case actually, so let's assume that some-function is expected to be defined when advice-add is evaluated. Thanks for pointing this out.
    – cYrus
    Nov 9, 2017 at 20:04
  • 2
    My rule of thumb for when to use #' is: use it if and only if (lambda (args...) (FOO args...)) could be used as well.
    – Stefan
    Nov 10, 2017 at 13:56
  • @Stefan so in this case it would be a "no"; advice-add wants a symbol and anyway that would advise a (anonymous) wrapper not the function itself, which makes little sense in this context.
    – cYrus
    Nov 10, 2017 at 14:51
  • 2
    That's right: it takes a symbol and not a function. As for "adivsing a wrapper" that can't be done: advising is about modifying something, so it applies to "generalized variables", aka "lvalues", but not to values.
    – Stefan
    Nov 10, 2017 at 15:54

1 Answer 1

0

Since #'foo is equivalent to (function foo) and the documentation states:

Like ‘quote’, but preferred for objects which are functions. In byte compilation, ‘function’ causes its argument to be compiled. ‘quote’ cannot do that.

IMHO the answer is yes, provided that the function to be advised is expected to be defined when the code is byte-compiled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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