Discussion: add more idiomatic versions of catchJust and/or handleJust
Oliver Charles
ollie at ocharles.org.uk
Thu Jul 14 16:56:21 UTC 2016
On Tue, 12 Jul 2016, 1:23 a.m. 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)
>
A point don't feel super strongly about, but feel I should raise, is that
Nothing seems to be somewhat confusing. catchJust (\FileNotFound ->
Nothing) seems to suggest that if FileNotFound occurs then nothing will
happen, that is - the exception is ignored. However, that is not the case,
rather than Nothing happening something certainly happens - an exception is
(re)thrown!
Whether or not this confusion is likely to happen in practice I don't know,
but it suggests a type isomorphic to Maybe is a better fit.
Ollie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160714/95a2b921/attachment.html>
More information about the Libraries
mailing list