Perhaps you want something like:

(with-backend [b (select-the-backend)]
  (do-something b))

and have with-backend call the appropriate init / finish methods?

Sean

On Fri, Oct 11, 2013 at 2:33 PM, Brian Craft <[email protected]> wrote:
> I'm working on module that may use a number of different backends, some of
> which may keep open files across a number of API calls. Other backends may
> not use files at all. Like
>
> (backend-init) ; opens some files, opaque to the caller
> (backend-do-something); uses the open files
> (backend-finish); close the files
>
> Off-hand, I don't see a way to structure code like this to use with-open,
> since the backend would lose its open files when returning from any
> with-open it might do.
>
> Am I approaching this the wrong way? Is there a standard way of structuring
> code like this? It's a bit like the backend is itself the resource to be
> opened (under the hood would be opening multiple files). I saw a thread
> about making a "close" multimethod that sounded related.
>
> Also, when the backend is opening files it will need to catch errors and
> close any opened files (e.g. if the second open fails, close the first),
> which is a bit like with-open, except it would call "close" only on error.
> Is this an existing method or idiom for this?
>
> I guess one alternative is to pass a body to be executed to the backend, so
> the backend can wrap it in with-open.
>
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.



-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.
  • with-open Brian Craft
    • Re: with-open Sean Corfield

Reply via email to