Proposal: Add Text.Read.maybeRead :: Read a => String -> Maybe a
dave at zednenem.com
Fri Feb 8 15:14:04 EST 2008
On Feb 8, 2008 4:56 AM, John Meacham <john at repetae.net> wrote:
> On Fri, Feb 08, 2008 at 12:17:31AM -0500, David Menendez wrote:
> > On Feb 7, 2008 6:12 PM, Ross Paterson <ross at soi.city.ac.uk> wrote:
> > > I think this is a good idea, though I normally write the equivalent of
> > >
> > > maybeRead :: Read a => String -> Maybe a
> > > maybeRead s = case reads s of
> > > [(x, rest)] | all isSpace rest -> Just x
> > > _ -> Nothing
> > >
> > > and would prefer not to generalize it to any monad. fail is a wart,
> > > not a design pattern.
> > 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, error messages in haskell are quite an issue as is, _any_
> useful information is good, passing them on to the user without
> interpretation is loads better than not having any clue what went wrong.
That's why I use "fromMaybe" (or "maybe"). Why narrow it down to a
call to some function when you can narrow it down to a specific call
to a function?
It's a pity there isn't something like __LINE__ in Haskell itself.
> It is what you want to do because the fact that it was a 'readM' that
> failed is extremely useful for figuring out what went wrong. It is a
> straightforward generalization that is very useful pragmatically.
We already have a function that produces an opaque error string to
pass to the user: read.
Dave Menendez <dave at zednenem.com>
More information about the Libraries