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)
Drew
  • 75,699
  • 9
  • 109
  • 225
ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • 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 04 '19 at 21:35

1 Answers1

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)
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • 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 04 '19 at 21:37