2

Is there a programmatic way to access function/macro arguments,

Something like this, which returns the the arg count and true if it accepts any number of additional arguments.

eg:

  • (number-of-arguments-and-rest "if") -> '(2, t)
  • (number-of-arguments-and-rest "progn") -> '(0, t)
  • (number-of-arguments-and-rest "null") -> '(1, nil)
  • Note: the right answer is usually "yes there is but it won't reliably do what you need". IOW it's an XY problem. – Stefan Dec 4 '19 at 21:35
3

You can use func-arity added in 26.1. C-h f func-arity:

func-arity is a built-in function in `C source code'.

(func-arity FUNCTION)

Return minimum and maximum number of args allowed for FUNCTION.
FUNCTION must be a function of some kind.
The returned value is a cons cell (MIN . MAX).
MIN is the minimum number of args.
MAX is the maximum number, or the symbol many, for a function with &rest args, or unevalled for a special form.

For example,

(func-arity 'null)
;; => (1 . 1)

(func-arity 'if)
;; => (2 . unevalled)

(func-arity 'progn)
;; => (0 . unevalled)

(func-arity '+)
;; => (0 . many)
  • 1
    Be aware that the returned value can be misleading. E.g. it may say (2 . many) when in reality it will fail when called with more than, say, 3 arguments. – Stefan Dec 4 '19 at 21:37

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.