unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* get nargs for a function
@ 2015-08-28 16:55 Sam Halliday
  2015-08-28 17:10 ` Ian Zimmerman
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Sam Halliday @ 2015-08-28 16:55 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,

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?

If I was just in dynamic scope, I could get cadr and count the size, but I have to support closure as well which is caddr (and however that changes in the future).

Best regards,
Sam


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
  2015-08-28 16:55 get nargs for a function Sam Halliday
@ 2015-08-28 17:10 ` Ian Zimmerman
  2015-08-29 21:41 ` Stefan Monnier
       [not found] ` <mailman.164.1440884507.19560.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Ian Zimmerman @ 2015-08-28 17:10 UTC (permalink / raw)
  To: help-gnu-emacs

On 2015-08-28 09:55 -0700, Sam Halliday 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?
 
> If I was just in dynamic scope, I could get cadr and count the size,
> but I have to support closure as well which is caddr (and however that
> changes in the future).

How does advice do it?

-- 
Please *no* private copies of mailing list or newsgroup messages.
Rule 420: All persons more than eight miles high to leave the court.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
  2015-08-28 16:55 get nargs for a function Sam Halliday
  2015-08-28 17:10 ` Ian Zimmerman
@ 2015-08-29 21:41 ` Stefan Monnier
       [not found] ` <mailman.164.1440884507.19560.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2015-08-29 21:41 UTC (permalink / raw)
  To: help-gnu-emacs

> 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
        ...do-something-else...))


-- Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
       [not found] ` <mailman.164.1440884507.19560.help-gnu-emacs@gnu.org>
@ 2015-08-29 22:41   ` Sam Halliday
  2015-08-30  2:22     ` Stefan Monnier
       [not found]     ` <mailman.178.1440901363.19560.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: Sam Halliday @ 2015-08-29 22:41 UTC (permalink / raw)
  To: help-gnu-emacs

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
>         ...do-something-else...))

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.

https://github.com/fommil/e99/blob/master/48.el

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
  2015-08-29 22:41   ` Sam Halliday
@ 2015-08-30  2:22     ` Stefan Monnier
       [not found]     ` <mailman.178.1440901363.19560.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2015-08-30  2:22 UTC (permalink / raw)
  To: help-gnu-emacs

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

To get the description of the arglist, you're better off using
`help-function-arglist'.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
       [not found]     ` <mailman.178.1440901363.19560.help-gnu-emacs@gnu.org>
@ 2015-08-30 14:14       ` Sam Halliday
  2015-08-31  0:17         ` Stefan Monnier
       [not found]         ` <mailman.227.1440980264.19560.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: Sam Halliday @ 2015-08-30 14:14 UTC (permalink / raw)
  To: help-gnu-emacs

On Sunday, 30 August 2015 03:22:46 UTC+1, Stefan Monnier  wrote:
> > (defun args (function)
> >   "The arg list of FUNCTION."
> >   (let ((kind (car function)))
> >     (if (eq kind 'closure)
> >         (cadr (cdr function))
> >       (cadr function))))
> 
> To get the description of the arglist, you're better off using
> `help-function-arglist'.

I don't believe that works here as the input is typically anonymous.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
  2015-08-30 14:14       ` Sam Halliday
@ 2015-08-31  0:17         ` Stefan Monnier
       [not found]         ` <mailman.227.1440980264.19560.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2015-08-31  0:17 UTC (permalink / raw)
  To: help-gnu-emacs

>> To get the description of the arglist, you're better off using
>> `help-function-arglist'.
> I don't believe that works here as the input is typically anonymous.

If it doesn't work, please report it as a bug.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: get nargs for a function
       [not found]         ` <mailman.227.1440980264.19560.help-gnu-emacs@gnu.org>
@ 2015-09-01 11:30           ` Sam Halliday
  0 siblings, 0 replies; 8+ messages in thread
From: Sam Halliday @ 2015-09-01 11:30 UTC (permalink / raw)
  To: help-gnu-emacs

On Monday, 31 August 2015 01:17:46 UTC+1, Stefan Monnier  wrote:
> >> To get the description of the arglist, you're better off using
> >> `help-function-arglist'.
> > I don't believe that works here as the input is typically anonymous.
> 
> If it doesn't work, please report it as a bug.

It does work, I just misunderstood the function documentation (I thought it took the name of a function, not the function itself). This is pretty much exactly what I wanted. Thanks!


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-09-01 11:30 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-28 16:55 get nargs for a function Sam Halliday
2015-08-28 17:10 ` Ian Zimmerman
2015-08-29 21:41 ` Stefan Monnier
     [not found] ` <mailman.164.1440884507.19560.help-gnu-emacs@gnu.org>
2015-08-29 22:41   ` Sam Halliday
2015-08-30  2:22     ` Stefan Monnier
     [not found]     ` <mailman.178.1440901363.19560.help-gnu-emacs@gnu.org>
2015-08-30 14:14       ` Sam Halliday
2015-08-31  0:17         ` Stefan Monnier
     [not found]         ` <mailman.227.1440980264.19560.help-gnu-emacs@gnu.org>
2015-09-01 11:30           ` Sam Halliday

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).