Skip to content

WIP: Fix 576

Fix #576 (closed), Related to !197 (merged), 72560efa

There is obviously a lot of redundancy in the new implementation. I tried to eliminate most of it and ended up with this interesting construct:

(defmacro with-output-to-string ((var &optional string &rest r &key element-type) &rest body)
  (multiple-value-bind (ds b)
      (find-declarations body)
    (let ((b (if string
                 b
                 `(,@b (get-output-stream-string ,var)))))
      `(let* (,@(if string
                    `((,var (make-string-output-stream-from-string ,string))
                      (,(gensym) ,element-type))
                    `((,var (make-string-output-stream ,@r)))))
         ,@ds
         (unwind-protect
              (progn
                ,@b)
           (close ,var))))))

but, while more concise, I am not convinced that it is equally comprehensible.

It also took me a few moments to fully realize that element-type is actually evaluated on both paths (as it should be), on the second as an argument to make-string-output-stream. As much as I appreciate the cleverness with the &rest keyword, I think there is a case to be made about whether the brevity achieved by reducing one keyword argument into a rest parameter is worth the potential for confusion in this case. But then again, my momentary irritation about this might not be a common phenomenon.

What do you think @jackdaniel ?

Merge request reports

Loading