This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Passing extra keywords to with-open-file() at runtime?

4 posts · 1405 views

Is it possible to add keywords to with-open-file() at runtime,ie. sometimes I'ld want :ifexists :supersede and sometimes not.

Right now I got
(defun serialize-hmm-model-to-file (file &rest kwords &key &allow-other-keys)
  (let ((s (apply #'open (append (list file :direction :output) kwords))))
    (serialize-hmm-model s)
    (close s)))
But it feels like to much of a mouthfull really.

-a

Re: Passing extra keywords to with-open-file() at runtime?

The value of the keyword argument is evaluated at runtime, so you can just use it. WITH-OPEN-FILE behaviour doesn't depend on the presence of its arguments, but gives them a default value, so there is no difference to not providing the argument and giving it an explicit value, which you probably should do anyway to make the program logic more clear.

Re: Passing extra keywords to with-open-file() at runtime?

You're right of course, the macro arguments are evaluated at runtime so there's no problem using with-open-file().

-a

Re: Passing extra keywords to with-open-file() at runtime?

sinnatagg wrote:
(defun serialize-hmm-model-to-file (file &rest kwords &key &allow-other-keys)
  (let ((s (apply #'open (append (list file :direction :output) kwords))))
    (serialize-hmm-model s)
    (close s)))
I know you now know you can just use with-open-file, but if you did want to do something like the above, you can just (apply #'open file :direction :output kwords) -- no need for that append junk. (You'd probably also want an unwind-protect in there, but that's another issue).