On 2024-01-18, Lawrence D'Oliveiro <
[email protected]d> wrote:
I often write long docstrings (not sure whether Lisp has its own name
for these).
Lis has its own name: docstrings. It is Python that doesn't have its own name.
(defmacro mstr (&rest strs)
"lets me define a long string literal in pieces across lines, useful for docstrings."
(apply 'concat strs)
What is concat?
) ; defmacro mstr
Another solution would be to make the macro take a format argument.
Common Lisp has a mechanism inside format strings for removing a newline
and eating the whitespace which follows: tilde before a newline.
(format nil "this is a ~
multi-line ~
print job that ~
becomes one line.")
But ...
And using this macro is equally easy:
(defun cur-line (ensure-newline)
(mstr
"returns list of two character positions, representing the"
" beginning and end of the selection if there is one, else"
" the beginning and end of the current line. ensure-newline"
" => ensures there is a newline at the end of the line."
)
I am concerned whether this is actually valid syntax for specifying
a docstring.
Note that according to ANSI CL, the syntax of defun is:
defun function-name lambda-list [[declaration* | documentation]] form*
You see how "declaration" and "form" are distinct? Further, the
specification says:
documentation---a string; not evaluated.
It's not evaluated, because it's not a form. The documentation
must be a string literal element in the defun syntax.
Your (mstr ...) therefore doesn't correspond to the "documentation"
element, it is a form.
Nowhere does the spec say that the forms enclosed in defun are
macroexpanded in order to look for docstrings.
It's indeed not working for me in CLISP.
If I define this:
(defun fn () "abc" 42)
the function has "abc" documentation. If I use this:
(defun fn () (mstr "abc") 42)
then it has NIL documentation.
(defmacro mstr () "abc")
MSTR
(mstr)
"abc"
(defun fn () (mstr) 42)
FN
(documentation 'fn 'function)
NIL
(defun fn () "abc" 42)
FN
(documentation 'fn 'function)
"abc"
You need to make your own defun-doc macro to make this work, or else
(yuck) rely on read-time #. evaluation.
;; using your concat
(defmacro defun-doc (name args docstring-list &body body)
`(defun ,name ,args ,(apply #'concat docstring-list) ,@body))
This macro sidesteps the problem by generating a defun which
a string object in the docstring position. defun nevers sees
anything but a string object there.
I would make the macro smart so that it checks for the missing
docstring-list, without which the first argument of the body will
be taken for that position. Like checking that docstring-list is
a proper list, all of whose elements are strings.
--
TXR Programming Language:
http://nongnu.org/txr
Cygnal: Cygwin Native Application Library:
http://kylheku.com/cygnal
Mastodon: @
[email protected]
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)