Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the using with with-open #7

Open
voldmar opened this issue Apr 26, 2017 · 2 comments
Open

Document the using with with-open #7

voldmar opened this issue Apr 26, 2017 · 2 comments

Comments

@voldmar
Copy link

voldmar commented Apr 26, 2017

Hello!

First of all, thanks for failjure

I would be glad if you write an example how to use failjure for cases, where a user needs to close opened resources in any case. E.g. I’m doing the next:

(f/attempt-all [reader (pdf-reader source)
                document (pdf-document reader)
                fields (fields document)]
  (do
    (.close document)
    (.close reader)
    fields)
  (f/when-fail [e]
    ; Here I do not have an access to reader and document
    ; to close them
  ))

I wonder if there any mechanism to close the document and the reader in any case, like with with-open

@voldmar
Copy link
Author

voldmar commented May 9, 2017

I made a temporary solution:

(defmacro with-f-open
  [bindings & body]
  (cond
    (= (count bindings) 0) `(do ~@body)
    (symbol? (bindings 0)) `(let ~(subvec bindings 0 2)
                              (if (f/failed? ~(bindings 0))
                                ~(bindings 0)
                                (try
                                  (with-f-open ~(subvec bindings 2) ~@body)
                                  (finally
                                    (. ~(bindings 0) close)))))

    :else (throw (IllegalArgumentException.
                   "with-f-open only allows Symbols in bindings"))))

After some reading, I think that the proper solution make some monad transformer or some magic like this

@adambard
Copy link
Owner

adambard commented May 9, 2017

Thanks for the sample! It seems to me that a monad-based solution would be the way to go, but that would probably require enough interop with algo.monads that failjure wouldn't be adding much (also, algo.monads doesn't work on clojurescript, which would be nice to have).

I'll do some experimentation sometime with cats, which is a monad library that does clojurescript too -- maybe a solution will present itself then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants