[Top][All Lists]

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

Re: get nargs for a function

From: Sam Halliday
Subject: Re: get nargs for a function
Date: Sat, 29 Aug 2015 15:41:30 -0700 (PDT)
User-agent: G2/1.0

On Saturday, 29 August 2015 22:41:49 UTC+1, Stefan Monnier  wrote:
> > Is there a built in way to return the arg list (actually just the number of
> > args) for a function that has been passed to me?
> The answer is "yes, but fundamentally no".
> The reason it's fundamentally no, is that you'd need to answer questions
> such as "how many arguments does cl-member take".
> In most cases, the better approach is to do something else, such as:
>     (condition-case
>         (funcall thefunction arg1 arg2 ...)
>       (wrong-number-of-arguments

This is only to answer a toy problem anyway so good to know this doesn't scale. 
I ended up doing this

(defun args (function)
  "The arg list of FUNCTION."
  (let ((kind (car function)))
    (if (eq kind 'closure)
        (cadr (cdr function))
      (cadr function))))

because the nature of the problem required working on a function with an 
arbitrary argument list.

I suppose I could re-interpret the question to take the nargs explicitly.

reply via email to

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