3

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)
1
  • 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, 2019 at 21:35

1 Answer 1

4

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
  • 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, 2019 at 21:37

Your Answer

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

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