Page 1 of 1

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

Posted: Sun Jul 24, 2011 7:32 am
by sinnatagg
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

Code: Select all


(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?

Posted: Sun Jul 24, 2011 9:33 am
by ramarren
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?

Posted: Sun Jul 24, 2011 10:25 am
by sinnatagg
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?

Posted: Sun Jul 24, 2011 8:54 pm
by Paul
sinnatagg wrote:

Code: Select all

(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).