2

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?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Mike Manilone
  • 582
  • 4
  • 17

2 Answers2

7

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    Why a poor implementation? Given the specs I'd say that CL code that relies on the ability to read back a docstring is poor code. – 6502 May 26 '13 at 15:27
  • 3
    Because people do more things with CL implementations than just run the code, they also need to write and debug with it. If an implementation ignores docstrings, it makes these tasks harder, so it's not as complete an implementation as ones that don't. – Barmar May 26 '13 at 19:59
1

As Barmar said, this is possible, but optional.

CLISP and SBCL support documentation strings for anonymous lambda functions.

CCL, ECL and LispWorks don't.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346