[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fixing optional arguments?

A standard problem with optional arguments in Common Lisp is that it
difficult to use the fact that a function was only called with n
arguments to call some other function with only n arguments.  For

(defun foo (x &optional (y nil y-p))
    (if y-p
        (bar x y)
        (bar x)))

This is a proposed solution to that problem.

- The default default value for an optional argument is the value of the
constant UNSUPPLIED-OPTIONAL-ARGUMENT (instead of nil).

So given the definition (defun foo (x &optional y) y).  (foo 1) would
return the value of the constant unsupplied-optional-argument.

- When function call sees the value of unsupplied-optional-argument
being used as an argument to a function, it treats it as an unsupplied
value for an optional argument.  As an important performance
optimization, any arguments following unsupplied-optional-argument are
also discarded.

The example becomes:

(defun foo (x &optional y)
  (bar x y) ..)

In addition, it is possible to write code like:

 (when <something>
    ;; We are only going to call baz with 2 arguments.
    (setq arg-3 unsupplied-optional-argument))
 (baz arg-1 arg-2 arg-3 arg-4)

In this example, the call to baz will be as if only two arguments were

The supplied-p variable stuff is also no longer needed since the same
thing can be determined by using (unsupplied-optional-argument-p <arg>).