Discussion: add more idiomatic versions of catchJust and/or handleJust

David Menendez dave at zednenem.com
Thu Jul 14 16:11:31 UTC 2016


On Mon, Jul 11, 2016 at 8:23 PM, David Feuer <david.feuer at gmail.com> wrote:

> The catchJust and handleJust functions seem a bit weird and unidiomatic.
>
> catchJust
>         :: Exception e
>         => (e -> Maybe b)         -- ^ Predicate to select exceptions
>         -> IO a                   -- ^ Computation to run
>         -> (b -> IO a)            -- ^ Handler
>         -> IO a
> catchJust p a handler = catch a handler'
>   where handler' e = case p e of
>                         Nothing -> throwIO e
>                         Just b  -> handler b
>
> This takes two functions and then puts them together. I would think the
> more natural API would be
>
> catchMaybe :: Exception e => IO a -> (e -> Maybe (IO a)) -> IO a
> catchMaybe m handler = catch m handler' where
>   handler' e = fromMaybe (throwIO e) (handler e)
>
> This is exactly as powerful as catchJust:
>
> catchMaybe m handler = catchJust handler m id
> catchJust p m handler = catchMaybe m $ fmap handler . p
>
> But catchMaybe doesn't enforce the arbitrary separation between
> "selection" and "handling”.
>
I wouldn’t call it an arbitrary separation. I imagine that the separation
is deliberate, because you will frequently want to catch the same set of
exceptions, but want to do different things in different contexts. With the
current API, you can write a selector like “fileNotFound” and re-use it
with multiple handlers.

In constrast, with a unified seletor/handler you have to write the same
code to filter the exceptions you want with every handler which deals with
them. You might as well just use catch.

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160714/601fd951/attachment.html>


More information about the Libraries mailing list