emacs-devel
[Top][All Lists]
Advanced

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

Arbitrary function: find the number(s) of expected arguments


From: Paul Pogonyshev
Subject: Arbitrary function: find the number(s) of expected arguments
Date: Tue, 15 Mar 2016 19:48:07 +0100

Hi,

Suppose I have a library with

    (defun my-foo (callback)
      ...
      (funcall callback computed-data))

Now, _some_ users of my library found out that it would be nice to have `this-value-gets-computed-as-side-effect' too, though it is not really necessary for callbacks in general. I wouldn't want to change interface of the library, instead I would just improve it like this:

    (defun my-foo (callback)
      ...
      (if (accepts-2-arguments callback)
          (funcall callback computed-data this-value-gets-computed-as-side-effect)
        (funcall callback computed-data)))

This way `my-foo' automagically accepts callbacks that expect one argument (as in initial form), as well as those that want two arguments.

The problem, of course, is to write `accepts-2-arguments'.  I found `subr-arity', which provides exactly what I need, but only for builtins... I was also told about `help-function-arglist', but it returns the raw argument list in which you still have to handle &optional or &rest (maybe something else too?). This looks like a bit too much work for simple thing and likely not good for performance-critical places either.

Feature request: add a builtin like `subr-arity' that works for _any_ function. Evaluator sure knows how to call the functions, so the information is already there.

Paul

reply via email to

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