convert ENSURE-GETHASH into a macro
authorRyan Davis <ryan@acceleration.net>
Wed, 21 Dec 2011 18:20:47 +0000 (13:20 -0500)
committerNikodemus Siivola <nikodemus@random-state.net>
Fri, 20 Jan 2012 14:56:56 +0000 (16:56 +0200)
 Evaluate the default-form only if we actually use it.

hash-tables.lisp

index a574737..65253e7 100644 (file)
@@ -89,11 +89,11 @@ PLIST. Hash table is initialized using the HASH-TABLE-INITARGS."
       (setf (gethash (car tail) table) (cadr tail)))
     table))
 
-(defun ensure-gethash (key hash-table &optional default)
+(defmacro ensure-gethash (key hash-table &optional default)
   "Like GETHASH, but if KEY is not found in the HASH-TABLE saves the DEFAULT
 under key before returning it. Secondary return value is true if key was
 already in the table."
-  (multiple-value-bind (value ok) (gethash key hash-table)
-    (if ok
-        (values value ok)
-        (values (setf (gethash key hash-table) default) nil))))
+  `(multiple-value-bind (value ok) (gethash ,key ,hash-table)
+     (if ok
+         (values value ok)
+         (values (setf (gethash ,key ,hash-table) ,default) nil))))