3

I'd like to be able to do:

(function-arity (intern "expt"))

I googled up this solution https://github.com/emacsmirror/parser/blob/master/parser-fn.el

But it's using help-function-arglist, so it's not exactly straightforward. I'd like something more solid, preferably rock-solid.

abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • 1
    I am curious... why do you find `help-function-arglist` not rock-solid? It manages functions, macros, aliases, autoloads,... – juanleon Jul 17 '13 at 07:33
  • I have no problem with `help-function-arglist` - it does its job very well. But getting the number of arguments by processing `help-function-arglist` output looks to me like parsing the manual, or a google request. – abo-abo Jul 17 '13 at 07:50
  • Would you feel differently if the name of the function was `function-arglist`? :-) IMHO the help in the name is somewhat incidental because of the file where it was put (there is a comment in the source code that says that the function may be moved to subr.el). That being said, the source code of that function hints me that getting the arity of a elisp function is not very straightforward, but I am no expert in that. – juanleon Jul 17 '13 at 07:58
  • I was referring to the fact that the `function-arglist` from the github reference just *parses* the output of a standard built-in function `help-function-arglist`. I'm looking for a built-in function that returns the arity, or a function that uses built-ins, but *without* parsing. For example, a new elisp keyword, like `&supercool` might come out in Emacs25, and the function that *parses* `help-function-arglist` to get the arity will become confused. – abo-abo Jul 17 '13 at 08:21
  • 1
    I've come across a more extreme approach than *parsing* out the arity: call the function multiple times with different parameters and see if it returns an error. I was rolling on the floor:) – abo-abo Jul 17 '13 at 08:26
  • Well, you should ignore all the "&\w". By the way, it seems common lisp has the same approach for finding the arity: http://stackoverflow.com/questions/15465138/find-functions-arity-in-common-lisp – juanleon Jul 17 '13 at 09:13
  • @abo-abo: You might have been rolling on the floor, but that's the right approach in code that must work also with older Emacs versions, where there is no good way to test arity. And yes, it's too bad that Emacs doesn't have a function such as `function-arity` in general. There is `subr-arity`, but it is only for built-ins. – Drew Oct 01 '13 at 22:29

2 Answers2

3

There's no such thing as a rock-solid function-arity. In most cases where people want it, what they really want is to call a function in different ways depending on how many arguments it accepts (because its signature is different in different Emacs versions, for example), in which case the approach of "try calling it with many arguments and catch the potential error" is about as good as it gets.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • In my case, I'm trying to parse `"exptxy"` to `(expt x y)`. And the point is that it shouldn't try to parse it as `(exp t x y)`, because `exp` has arity 1. – abo-abo Jul 22 '13 at 16:05
1

Can't see anything wrong WRT help-function-arglist, which does it's job here.

While lambda-arity --pointed at-- seems to have some bugs still.

For example

(optional-arglist (memq '&optional arglist))

should rather use `member '&optional', which would return the remaining list, thus enable counting the elements left.

Andreas Röhler
  • 4,804
  • 14
  • 18