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

Discussion of Common Lisp
Post Reply
sinnatagg
Posts: 29
Joined: Tue Apr 21, 2009 3:04 am

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

Post by sinnatagg » Sun Jul 24, 2011 7:32 am

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

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

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

Post by ramarren » Sun Jul 24, 2011 9:33 am

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.

sinnatagg
Posts: 29
Joined: Tue Apr 21, 2009 3:04 am

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

Post by sinnatagg » Sun Jul 24, 2011 10:25 am

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

-a

Paul
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

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

Post by Paul » Sun Jul 24, 2011 8:54 pm

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

Post Reply