Proposal: Add Text.Read.maybeRead :: Read a => String -> Maybe a

Don Stewart dons at galois.com
Fri Feb 8 12:11:20 EST 2008


ross:
> On Fri, Feb 08, 2008 at 01:56:27AM -0800, John Meacham wrote:
> > On Fri, Feb 08, 2008 at 12:17:31AM -0500, David Menendez wrote:
> > > I also prefer Maybe to fail. Error strings are only useful if you're
> > > ignoring them or passing them to the user without interpretation.
> > 
> > say that next time you get a mysterious "fromJust: Nothing" error with
> > no context,
> 
> And that is why people recommend against using functions that throw
> errors, like fromJust.  But in some monads (and which monad is involved
> isn't immediately obvious), readM would be another such function (as is
> read, of course).
> 
> In contrast, the proposed maybeRead wraps up a solution to a common
> problem in a safe way, and requires the caller (who has more information
> about the context) to handle failed parses.

Quite so.

The whole point of this exercise was to get a version of read that was
safe, and where an unintential <- or other slip up wouldn't bring down
the system.

readM with fail defaults to ioError for almost all Monads, and so admits
many dangerous programs, which is against the intent of the proposal 
in the first place.

It seems with Conor's suggestion of a 

    maybeReturn :: MonadPlus m => Maybe a -> m a
    maybeReturn = maybe mzero return

we can still have the by-default-safe maybeRead, that doesn't admit 
exception throwing opportunities.

-- Don


More information about the Libraries mailing list