2

If a function takes a parameter whose only significance is in whether it's nil or non-nil, is there any reason not to pass it a well-named symbol to clarify what's going on in the calling code?

For example, add-to-list takes an optional APPEND argument, which if non-nil adds the element to the end of the list rather than the beginning. I could write,

(add-to-list my-list "x" t)

but I think it would be clearer if I wrote

(add-to-list my-list "x" 'append)

or

(add-to-list my-list "x" :append)

I like that last one best, but is that just not done? I could actually see it being confusing since it makes it look like a keyword argument and it's not really.

1
  • 1
    Actually, what is usually called a "keyword argument" in Lisp refers to the use of keyword arguments a la Common Lisp. And there you pass both the keyword and its value. The point of such (real) keyword arguments is that you can specify them in any order (after required arguments). In the case you show, you are just passing a keyword symbol as an ordinary argument (not as a keyword argument, in the usual sense).
    – Drew
    Aug 14, 2016 at 1:43

2 Answers 2

5

No, there is no reason not to pass a non-nil value that has some other meaning (to a human reader, if not to the executing code).

It is common to do this, especially when there are multiple parameters, and especially if some are used in some call which are not used very often. That can help a (human) reader understand without needing to use C-h f to consult the doc to see what a given parameter means.

You can use a quoted symbol - e.g. 'FOO. You can use a keyword symbol - e.g. :FOO.


Beyond this documenting role, a function that you define can have different behaviors for different non-nil values, and yet have all of those different values have something in common (or not), different from the behavior of a nil value.

2

Yes you can. You can use any value which isn't nil (noting that an empty list is the same thing as nil).

That's precisely why the docs invariably use that awkward "non-nil" wording.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.