1

I am looking for something that could be used like this:

(when (cl-applicable-method-p 'foo "arg") ...)

I vaguely remember that I have seen that once and that it came with a comment saying that one shouldn't use it. However I would like to at least investigate this approach, which I cannot do if I cannot guess the name and am unable to find the implementation. (But please let me know what is wrong with using such a predicate, if anything.)

I cannot just call the generic function and prepare to handle cl-no-applicable-method errors or use a no-op default-method because I have to do some additional work before and after calling the generic function, but only if it actually is implemented given the arguments I intend to call it with

Actually the "argument" is the current major-mode, the arglist of the methods is (&context (major-mode MODE)), which probably complicates this further.

It would be nice if I could do something like:

(when (cl-applicable-method-p 'foo &context major-mode)
  (prepare)
  (foo)
  (finish))

Another approach would be:

(defmacro foo-common (&rest body)
  `(progn (prepare)
          ,@body
          (finish)))

(cl-defgeneric foo ()
  nil) ; noop

(cl-defmethod foo (&context (major-mode emacs-lisp-mode))
  (foo-common
   (mode-specific-stuff)))

but that feels hackish, it would be nicer if foo-common could be replaced with a foo :around method.


I am trying to replace the various magit-...-refresh-buffer functions used in magit-refresh-buffer with a generic function.

4
  • I think the closest I can think of is cl-next-method-p, but I'd indeed recommend to stay away from it. Maybe we could suggest a better solution if you explained what you're hoping to gain from the use of a generic-function for magit-refresh-buffer?
    – Stefan
    Mar 14, 2019 at 14:51
  • I don't particularly like (funcall (intern (concat ... "-refresh-buffer"))) and I hope for other small benefits. Nothing major, just consistency with other changes where using a generic function poses no problems.
    – tarsius
    Mar 14, 2019 at 15:16
  • What I am mostly interested in is to learn why I shouldn't do this. Is it un-clos-y to ask "does this exist?" instead of calling the function and see what happens, or does the reason have more to do with implementation details?
    – tarsius
    Mar 14, 2019 at 15:18
  • One way to look at the problem is that the code that signals "no such method" is basically itself a default method, and if you check for existence of a method you might find a "spurious" method (e.g. there's a :before method but no primary method, or there's a dummy default method which just signals some other error). In addition, I actively discourage use of cl-next-method-p because its implementation is horribly hackish and brittle (but implementing it in a robust way would be a lot more expensive for a feature that's basically never used but would cost even when you don't use it).
    – Stefan
    Mar 14, 2019 at 22:02

0

Your Answer

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

Browse other questions tagged or ask your own question.