Question

I know that def* can have docstrings, so I just give lambdas a try.

Embarrassingly, The following returns NIL.

(documentation (lambda () "a function which always returns nil" nil) 'function)

What's wrong? Can't lambdas have docstrings? Is there a way to do it?

Was it helpful?

Solution

According to the HyperSpec section Symbol LAMBDA, the syntax is:

lambda lambda-list [[declaration* | documentation]] form*

so a doc-string is clearly allowed. Standard Generic Function DOCUMENTATION, (SETF DOCUMENTATION) lists two of the standard method signatures as:

documentation (x function) (doc-type (eql 't))
documentation (x function) (doc-type (eql 'function))

which match the way you tried to call it.

However, the function description includes the following caveat:

Documentation strings are made available for debugging purposes. Conforming programs are permitted to use documentation strings when they are present, but should not depend for their correct behavior on the presence of those documentation strings. An implementation is permitted to discard documentation strings at any time for implementation-defined reasons.

So while it's not technically a bug that the doc strings are not saved in your case, it's a poor implementation.

OTHER TIPS

As Barmar said, this is possible, but optional.

CLISP and SBCL support documentation strings for anonymous lambda functions.

CCL, ECL and LispWorks don't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top