Why doesn’t Haskell’s Prelude.read return a Maybe?

Edit: As of GHC 7.6, readMaybe is available in the Text.Read module in the base package, along with readEither: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Text-Read.html#v:readMaybe


Great question! The type of read itself isn’t changing anytime soon because that would break lots of things. However, there should be a maybeRead function.

Why isn’t there? The answer is “inertia”. There was a discussion in ’08 which got derailed by a discussion over “fail.”

The good news is that folks were sufficiently convinced to start moving away from fail in the libraries. The bad news is that the proposal got lost in the shuffle. There should be such a function, although one is easy to write (and there are zillions of very similar versions floating around many codebases).

See also this discussion.

Personally, I use the version from the safe package.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)