Nyquist / XLISP 2.0  -  Contents | Tutorials | Examples | Reference

cl:*multiple-values*


The cl:*multiple-values* variable is used to signal if a function has returned multiple values:

cl:*multiple-values*
returns -  T  if a function returned multiple values

Test if a function has returned multiple values:

(setf cl:*multiple-values* nil)
(let ((result (function ...)))
  (if cl:*multiple-values*
      (do-something-with *rslt* ...)
      (do-something-with result ...))
  ... )

Do not use cl:*multiple-values* with let like this:

(let ((cl:*multiple-values* nil)
      (result (function ...)))
  (if cl:*multiple-values*
      (do-something-with *rslt* ...)
      (do-something-with result ...))
  ... )

This doesn't work because 'function' is evaluated in the global XLISP environment, where the lexical let binding of the cl:*multiple-values* variable does not exist, while the  if  form inside the let form cannot see a global change of the cl:*multiple-values* variable, because the global value is shadowed by the lexical let binding. See Environment for more details about variables.

The XLISP progv special form can be used to encapsulate a multiple value call while automatically restoring the old values at the end like this:

(values 1 2 3)        => 1

cl:*multiple-values*  => T
*rslt*                => (1 2 3)

(progv '(cl:*multiple-values* *rslt*) '(nil nil)
  (let ((result (function ...)))
    (if cl:*multiple-values*
        (do-something-with *rslt* ...)
        (do-something-with result ...))))

cl:*multiple-values*  => T
*rslt*                => (1 2 3)

Note: All functions returning multiple values set cl:*multiple-values* to  T , but it's up to the Lisp programmer to reset the variable to NIL.

  Back to top


Nyquist / XLISP 2.0  -  Contents | Tutorials | Examples | Reference