Macro ENSURE

From: Nicolas Neuss (my.name_at_iwr.uni-heidelberg.de)
Date: 07/30/04


Date: 30 Jul 2004 13:51:03 +0200

Hello.

Relatively often I happen to write

(unless x (setf x y))

or

(setf x (or x y))

and I want to replace this with a macro ENSURE as

(ensure x y)

Since the macro

(defmacro ensure (x y)
 `(setf ,x (or ,x ,y)))

has problems with multiple evaluation (e.g. (ensure (aref x (incf i)) 1)),
the best solution should be something like

(define-modify-macro ensure (&rest args) or)

Question 1: Is this allowed by the spec, i.e. is the macro OR allowed to
appear at this place?

Question 2: Is this macro a good idea?

Question 3: Since such a macro might be rather common: is there another
name for it?

Thanks, Nicolas.



Relevant Pages

  • Re: memory usage in cmucl
    ... `(setf state,statename)) ... This macro is just taking up space. ... I don't understand why "a more elegant state machine notation" ... (defparameter driver-c nil) ...
    (comp.lang.lisp)
  • Re: reactivate SET?
    ... Well, it seems that for that purpose, the existing SETQ (is this one ... or SETF is likely to be sufficient. ... for such a new macro. ... (setq var 'bar) ...
    (comp.lang.lisp)
  • Re: macro-problem
    ... (setf var val)) ... Quote: `(setf,var,val) ... your macro has to return the s-expression ... The setf form inside your macro binds the local variable ...
    (comp.lang.lisp)
  • Re: rotatef inner workings (newbie)
    ... (setf a 0) ... DEFUN defines a function, not a macro. ... of ROTATEF it's what you need to do. ... (res (list 'LET* nil nil)) ...
    (comp.lang.lisp)
  • Re: Request for help constructing a simple macro
    ... (setf b (pop list)) ... A macro cannot expand to a sequence of forms. ... '(concept paradigm idea) ...
    (comp.lang.lisp)