* How many parameters does an elisp function take?
@ 2005-02-16 20:11 Alan Mackenzie
2005-02-16 20:45 ` Kevin Rodgers
2005-02-18 13:59 ` Stefan Monnier
0 siblings, 2 replies; 7+ messages in thread
From: Alan Mackenzie @ 2005-02-16 20:11 UTC (permalink / raw)
Is it possible to determine at run time how many parameters an elisp
function takes? For example, I'd like to write something like:
(how-many-params 'null)
and have it evaluate to 1. Or something like that. Together with some
reasonable convention for indicating &optional and &rest arguments.
--
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How many parameters does an elisp function take?
2005-02-16 20:11 How many parameters does an elisp function take? Alan Mackenzie
@ 2005-02-16 20:45 ` Kevin Rodgers
2005-02-16 21:02 ` David Kastrup
2005-02-18 13:59 ` Stefan Monnier
1 sibling, 1 reply; 7+ messages in thread
From: Kevin Rodgers @ 2005-02-16 20:45 UTC (permalink / raw)
Alan Mackenzie wrote:
> Is it possible to determine at run time how many parameters an elisp
> function takes? For example, I'd like to write something like:
>
> (how-many-params 'null)
>
> and have it evaluate to 1. Or something like that. Together with some
> reasonable convention for indicating &optional and &rest arguments.
I would start with eldoc-function-arglist.
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How many parameters does an elisp function take?
2005-02-16 20:45 ` Kevin Rodgers
@ 2005-02-16 21:02 ` David Kastrup
2005-02-16 23:56 ` Kevin Rodgers
0 siblings, 1 reply; 7+ messages in thread
From: David Kastrup @ 2005-02-16 21:02 UTC (permalink / raw)
Kevin Rodgers <ihs_4664@yahoo.com> writes:
> Alan Mackenzie wrote:
>> Is it possible to determine at run time how many parameters an elisp
>> function takes? For example, I'd like to write something like:
>> (how-many-params 'null)
>> and have it evaluate to 1. Or something like that. Together with
>> some
>> reasonable convention for indicating &optional and &rest arguments.
>
> I would start with eldoc-function-arglist.
For built-in functions, subr-arity might help.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How many parameters does an elisp function take?
2005-02-16 21:02 ` David Kastrup
@ 2005-02-16 23:56 ` Kevin Rodgers
0 siblings, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2005-02-16 23:56 UTC (permalink / raw)
David Kastrup wrote:
> Kevin Rodgers <ihs_4664@yahoo.com> writes:
>>Alan Mackenzie wrote:
>>>Is it possible to determine at run time how many parameters an elisp
>>>function takes? For example, I'd like to write something like:
>>>(how-many-params 'null)
>>>and have it evaluate to 1. Or something like that. Together with
>>>some
>>>reasonable convention for indicating &optional and &rest arguments.
>>
>>I would start with eldoc-function-arglist.
>
> For built-in functions, subr-arity might help.
And now for lisp functions, lambda-arity:
(require 'eldoc)
(defun lambda-arity (function)
"Return minimum and maximum number of args allowed for FUNCTION.
FUNCTION must be a symbol whose function binding is a lambda expression
or a macro.
The returned value is a pair (MIN . MAX). MIN is the minimum number
of args. MAX is the maximum number or the symbol `many', for a lambda
or macro with `&rest' args."
(let* ((arglist (eldoc-function-arglist function))
(optional-arglist (memq '&optional arglist))
(rest-arglist (memq '&rest arglist)))
(cons (- (length arglist)
(cond (optional-arglist (length optional-arglist))
(rest-arglist (length rest-arglist))
(t 0)))
(cond (rest-arglist 'many)
(optional-arglist (+ (length arglist)
(length optional-arglist)
-1))
(t (length arglist))))))
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How many parameters does an elisp function take?
2005-02-16 20:11 How many parameters does an elisp function take? Alan Mackenzie
2005-02-16 20:45 ` Kevin Rodgers
@ 2005-02-18 13:59 ` Stefan Monnier
2005-02-18 17:43 ` Alan Mackenzie
1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2005-02-18 13:59 UTC (permalink / raw)
> Is it possible to determine at run time how many parameters an elisp
> function takes? For example, I'd like to write something like:
> (how-many-params 'null)
> and have it evaluate to 1. Or something like that. Together with some
> reasonable convention for indicating &optional and &rest arguments.
Why do you want to know?
Every time this has shown up for me, what I truly wanted to know was more
like "can I call this with 4 args?", and the reason why I wanted to know was
to know whether to call it with 4 args or otherwise do something else
(e.g. call it with fewer args).
In practice, it's simpler to just do
(condition-case nil
(fooo)
(wrong-number-of-arguments
(bar)))
It's not perfect, but I've found it to suffer from fewer problems than
other solutions. It's also faster.
Stefan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How many parameters does an elisp function take?
2005-02-18 13:59 ` Stefan Monnier
@ 2005-02-18 17:43 ` Alan Mackenzie
2005-02-22 14:41 ` Stefan Monnier
0 siblings, 1 reply; 7+ messages in thread
From: Alan Mackenzie @ 2005-02-18 17:43 UTC (permalink / raw)
Stefan Monnier <monnier@iro.umontreal.ca> wrote on Fri, 18 Feb 2005
08:59:24 -0500:
>> Is it possible to determine at run time how many parameters an elisp
>> function takes? For example, I'd like to write something like:
>> (how-many-params 'null)
>> and have it evaluate to 1. Or something like that. Together with
>> some reasonable convention for indicating &optional and &rest
>> arguments.
> Why do you want to know?
I'd like to fix `beginning-of-defun-raw', where it does (funcall
beginning-of-defun-function). The parameter `arg' from
beginning-of-defun should be passed through to b-o-d-f. We've talked
about this before. Unfortunately, there is no guarantee that existing
user supplied functions can accept an `arg'. I would thus replace the
call with
(if (>= (how-many-params beginnin-of-defun-function) 1)
(funcall beginning-of-defun-function arg)
(funcall beginning-of-defun-function))
> Every time this has shown up for me, what I truly wanted to know was
> more like "can I call this with 4 args?", and the reason why I wanted
> to know was to know whether to call it with 4 args or otherwise do
> something else (e.g. call it with fewer args).
Exactly.
> In practice, it's simpler to just do
> (condition-case nil
> (fooo)
> (wrong-number-of-arguments
> (bar)))
> It's not perfect, but I've found it to suffer from fewer problems than
> other solutions. It's also faster.
Good idea!
Why is there not such a function in the Emacs core? It seems such an
incredibly useful function, say for debuggers or code-analysers, or for
the uses mentioned above. Did somebody just overlook it in the early
days, perhaps?
> Stefan
--
Alan Mackenzie (Munich, Germany)
Email: aacm@muuc.dee; to decode, wherever there is a repeated letter
(like "aa"), remove half of them (leaving, say, "a").
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: How many parameters does an elisp function take?
2005-02-18 17:43 ` Alan Mackenzie
@ 2005-02-22 14:41 ` Stefan Monnier
0 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2005-02-22 14:41 UTC (permalink / raw)
>> In practice, it's simpler to just do
>> (condition-case nil
>> (fooo)
>> (wrong-number-of-arguments
>> (bar)))
>> It's not perfect, but I've found it to suffer from fewer problems than
>> other solutions. It's also faster.
> Good idea!
> Why is there not such a function in the Emacs core?
The example code above shows there *is* such a functionality. If you mean
"why is there no `function-can-accept-N-args' function", then the answer is
probably that most people ask for the inconvenient "function-arity" instead
and get turned down.
> It seems such an incredibly useful function, say for debuggers or
> code-analysers, or for the uses mentioned above. Did somebody just
> overlook it in the early days, perhaps?
Seeing how this kind of thing is basically never used, and how the specific
info needed tends to be subtly different each time, I guess it's not just
an overlook, but a lack of clear evidence of a need.
Stefan
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2005-02-22 14:41 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-02-16 20:11 How many parameters does an elisp function take? Alan Mackenzie
2005-02-16 20:45 ` Kevin Rodgers
2005-02-16 21:02 ` David Kastrup
2005-02-16 23:56 ` Kevin Rodgers
2005-02-18 13:59 ` Stefan Monnier
2005-02-18 17:43 ` Alan Mackenzie
2005-02-22 14:41 ` Stefan Monnier
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).