tag:blogger.com,1999:blog-6655746112052720933.post5740695380314042812..comments2022-03-30T18:43:33.525-05:00Comments on Notes on Haskell: Design Patterns in Haskell: bracketAdam Turoffhttp://www.blogger.com/profile/11941071792943377879noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6655746112052720933.post-12800651391289051552007-03-12T10:53:00.000-05:002007-03-12T10:53:00.000-05:00The first commentor wasn't paying attention appare...The first commentor wasn't paying attention apparently. The OP wasn't saying it isn't needed at all. He was saying that you don't have to write the exception code as in Java. It has been generalized to a function 'bracket'.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6655746112052720933.post-90180067800721521642007-03-12T09:17:00.000-05:002007-03-12T09:17:00.000-05:00C++ bases all of its resource management on this i...C++ bases all of its resource management on this idea, in the form of RAII ("Resource Acquisition Is Initialization"), a very awkwardly named yet powerful resource management idiom based on the idea of acquiring resources in constructors and releasing them in destructors. C++ guarantees that destructors are automatically called for variables when they go out of scope, thereby <I>transparently</I> ensuring proper cleanup - even in the face of exceptions.<BR/><BR/>One thing that is annoying about the Haskell "withFile"-style solutions is that it isn't transparent: the user must remember to use them. Another thing that is annoying is that every resource type comes with its own "withFOO" function. This latter annoyance, however, we can easily get rid of using a type class:<BR/><BR/>> class IOResource a where dealloc :: a -> IO ()<BR/>> <BR/>> withResource :: IOResource a => IO a -> (a -> IO b) -> IO b<BR/>> withResource x = bracket x dealloc<BR/><BR/>Now, we can say for example:<BR/><BR/>> instance IOResource Socket where dealloc = sClose<BR/><BR/>This way a user needs to remember neither that Sockets need to be closed with sClose, nor what "with..." function is to be used with Sockets. All he needs to remember is that a Socket is an IOResource, and that he can therefore use withResource:<BR/><BR/>> withResource (socket AF_INET Stream 6) $ \sock -> do ...<BR/><BR/>The reason we can apply this generalization is that resource deallocation functions normally only take a single argument, namely the object representing the resource. This corresponds to the fixed signature for destructors in C++.<BR/><BR/>"withResource" is still not as nice as C++'s destructors, but it's getting closer.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-6655746112052720933.post-53451410738539301752007-03-06T03:11:00.000-05:002007-03-06T03:11:00.000-05:00Looks a lot like withFile, available in the darcs ...Looks a lot like withFile, available in the darcs version of the base library:<BR/><BR/><BR/>withFile name mode =<BR/> bracket (openFile name mode) hCloseDon Stewarthttps://www.blogger.com/profile/08476737262404343154noreply@blogger.comtag:blogger.com,1999:blog-6655746112052720933.post-6417218932203919162007-03-06T02:19:00.000-05:002007-03-06T02:19:00.000-05:00But Haskell does have macros, with support for ear...But Haskell does have <A HREF="http://haskell.org/th/" REL="nofollow">macros</A>, with support for early and late bindings, type checking for the macro AST, quasiquoting and splicing. It's very useful -- we even use some of them in Pugs. :-)鳳https://www.blogger.com/profile/07500060847849562421noreply@blogger.comtag:blogger.com,1999:blog-6655746112052720933.post-52267072014540186502007-03-06T00:01:00.000-05:002007-03-06T00:01:00.000-05:00To ensure that your version of bracket releases th...To ensure that your version of <I>bracket</I> releases the acquired resource in the event the <I>action</I> fails, you'll need to add code to catch any exception that occurs, release the resource, and then re-throw the exception. In effect, you <EM>do</EM> need the Haskell equivalent of a try/finally block.<BR/><BR/>Here's how the libraries do it:<BR/><BR/>http://darcs.haskell.org/packages/haskell98/IO.hs<BR/>http://darcs.haskell.org/packages/base/Control/Exception.hs<BR/><BR/>Cheers,<BR/>TomAnonymousnoreply@blogger.com