Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining function argument list in Common Lisp

Is it possible to find out the argument list of a function, given a function object (or a function's symbol) in common lisp?

like image 438
sabof Avatar asked Dec 02 '11 08:12

sabof


2 Answers

This is different for each CL implementation but the Swank package (provides Slime which can show arglists in f.e. Emacs' minibuffer) wraps this up in a single function:

* (defun testfn (arg1 arg2 &key (arg3 :a)) (declare (ignore arg1 arg2 arg3)))
TESTFN
* (swank-backend:arglist #'testfn)
(ARG1 ARG2 &KEY (ARG3 :A))

This will also work for methods:

* (defmethod testmethod ((arg1 t) arg2 &key (arg3 :a)) (declare (ignore arg1 arg2 arg3)))
STYLE-WARNING: Implicitly creating new generic function TESTMETHOD.
#<STANDARD-METHOD TESTMETHOD (T T) {1005670231}>
* (swank-backend:arglist #'testmethod)
(ARG1 ARG2 &KEY (ARG3 :A))

The easiest way to get Swank is to use Quicklisp.

like image 151
aerique Avatar answered Oct 05 '22 20:10

aerique


ANSI Common Lisp provides the function FUNCTION-LAMBDA-EXPRESSION, which may return a lambda expression if the implementation supports it and the expression has been recorded. In the lambda expression, the second item is the argument list - as usual.

Otherwise to return an argument list is not defined in the ANSI Common Lisp standard and is part of the specific Lisp implementation. For example in some 'delivered' Lisp applications this information may not be present.

Typically most Common Lisp implementations will have an exported function ARGLIST in some internal package.

like image 31
Rainer Joswig Avatar answered Oct 05 '22 18:10

Rainer Joswig