bracket or how to release ressources.

Hello, I try to write a sort of withxxx method whcih release a file withH5File :: FilePath -> (HId_t -> IO r) -> IO r withH5File name = bracket (withCString name acquire) h5f_close where acquire file = h5f_open file h5f_ACC_RDONLY h5p_DEFAULT In my case h5f_open return a HId_t value (HId_t Int) If something went wrong the returned value is negativ. In that case I do not need to h5f_close the file but instead return an error My question is how should I write the bracket part when the finaliser (h5f_close) depends on the result of the acquire part. thanks for your help Frederic

If your finalization step depends on the result of your action, bracket is not an appropriate choice. It is for resources that are known at the time they are acquired. On Sat, Mar 26, 2016 at 11:59 AM PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Hello, I try to write a sort of withxxx method whcih release a file
withH5File :: FilePath -> (HId_t -> IO r) -> IO r withH5File name = bracket (withCString name acquire) h5f_close where acquire file = h5f_open file h5f_ACC_RDONLY h5p_DEFAULT
In my case h5f_open return a HId_t value (HId_t Int) If something went wrong the returned value is negativ. In that case I do not need to h5f_close the file but instead return an error
My question is how should I write the bracket part when the finaliser (h5f_close) depends on the result of the acquire part.
thanks for your help
Frederic _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Looking at the haskellforall I found something which should be interesting in my case A type dedicated to this sort of problem. the Resource Monad. I will investigate :) thanks Frederic [1] http://www.haskellforall.com/2013/06/the-resource-applicative.html

Hello, I end up with this sort of code using Maybe. withDataspace :: HId_t -> (Maybe HId_t -> IO r) -> IO r withDataspace hid f = bracket acquire release f where acquire = do space_id@(HId_t status) <- h5d_get_space hid return $ if status < 0 then Nothing else (Just space_id) release (Just shid) = h5s_close shid release Nothing = return (HErr_t (-1))
participants (2)
-
PICCA Frederic-Emmanuel
-
Rein Henrichs