Proposal: Add Text.Read.maybeRead :: Read a => String -> Maybe a
iavor.diatchki at gmail.com
Thu Feb 7 16:58:57 EST 2008
I don't think that the function should be in an arbitrary monad
because not all monads support failure, and the purpose of using this
function is to take an action when a parse error occurs (if parse
errors were not an issue, then we could just use 'read' instead). If
we really wanted to generalize the function, then we should use the
'MonadPlus' (or 'Alternative') classes to restrict the result to types
that have meaningful "default" values. Having said this, I have used
this function on many occasions, and the Maybe type has always been
sufficient, so my preference would be to keep the original type that
was proposed by Don.
2008/2/7 Jeff Polakow <jeff.polakow at db.com>:
> Why not allow an arbitrary monad?
> readM :: (Monad m, Read a) => String -> String -> m a
> readM errMsg s = case reads s of
> [(x, "")] -> return x
> _ -> fail errMsg
> libraries-bounces at haskell.org wrote on 02/07/2008 01:46:48 PM:
> > This function is typically defined once per project. So its
> > about time this safe variant of 'read' made it into the base.
> > maybeRead :: Read a => String -> Maybe a
> > maybeRead s = case reads s of
> > [(x, "")] -> Just x
> > _ -> Nothing
> > Consideration period: 1 week.
> > Patch to Text.Read attached.
> > [attachment "maybeRead.patch" deleted by Jeff Polakow/db/dbcom]
> > _______________________________________________
> > Libraries mailing list
> > Libraries at haskell.org
> > http://www.haskell.org/mailman/listinfo/libraries
> This e-mail may contain confidential and/or privileged information. If you
> are not the intended recipient (or have received this e-mail in error)
> please notify the sender immediately and destroy this e-mail. Any
> unauthorized copying, disclosure or distribution of the material in this
> e-mail is strictly forbidden.
> Libraries mailing list
> Libraries at haskell.org
More information about the Libraries