[alexandria-devel] MAYBECALL

Tobias C. Rittweiler tcr at freebits.de
Thu Jan 17 16:41:37 UTC 2008


I'd like to propose the addition of the following function:

  (defun maybecall (bool fn &rest args)
    "Call FN with ARGS if BOOL is T. Otherwise return ARGS as multiple values."
    (if bool (apply fn args) (values-list args)))

MAYBECALL is the general, or anonymous, solution to many ensure-foo
problems.

  (maybecall (not (listp thing)) #'list thing) == (ensure-list thing)

  (maybecall (listp thing) #'car thing) == (ensure-car thing)

I've thought about naming it FUNCALL-IF, but that could be read as
taking a predicate function a la all the sequence functions, rather than
a boolean value. And the result of making it take a predicate

  (funcall-if #'listp #'car thing)

is not so nice if you want the call not to depend directly on the
arguments passed. E.g. a use case where I often use MAYBECALL is to call
a function depending on a flag (most often argument to the surrounding
function.)

I.e.

   (maybecall removep #'remove foo bars)

vs.

   (funcall-if (constantly removep) #'remove foo bars)

That said, I don't care much which of these two you eventually decide upon.

  -T.




More information about the alexandria-devel mailing list