clojure - why doesn't with-open force evaluation -
just wondering why with-open
doesn't force evaluation of lazyseq
, yet prn
does?
with-open
doing side effects, isn't bad idea doing inside with-open produces lazy possible consumption later on? in situation this?
the 2 serve different purposes. prn
function designed print things in way can read reader. if thing being printed happens lazy sequence, thing force evaluation of lazy sequence in order print contents.
with-open
, on other hand, general-purpose macro designed execute arbitrary code in body in context resources closed on completion of code. it's syntactic sugar. macro knows nothing code in body doing, or sort of result it's going produce.
you correct returning lazy sequences with-open
try read already-closed resource common source of bugs in clojure programs. however, it's not feasible with-open
macro reliably detect when body causes lazy sequence produced , automatically force it. have aware of situation , handle in own code when arises.
Comments
Post a Comment