Proposal: Add readMaybe (and possibly readEither) to Prelude, make Haddocks for read more cautionary

Henning Thielemann lemming at henning-thielemann.de
Wed Dec 28 16:12:44 UTC 2016


On Wed, 28 Dec 2016, Simon Jakobi via Libraries wrote:

> read [1] is an easy way to introduce runtime exceptions into programs,
> but its documentation doesn't sufficiently warn of this danger. read's
> safe alternatives, Text.Read.readMaybe [2] and Text.Read.readEither
> [3], are relatively unknown and too hard to find.
>
> To guide users (particularly newcomers) away from read and towards
> readMaybe and readEither, I propose to
>
> 1. Add readMaybe to the Prelude
> 2. Add readEither to the Prelude

I do not like to add more stuff to Prelude. It is pretty cumbersome to 
write packages that equally work for Prelude versions before and after 
this change. In order to avoid preprocessor clutter I tend to import 
explicitly from Prelude or other 'base' modules. You can easily import 
these functions from Text.Read.

I would be ok with deprecating 'read'.

Generally I think that the Read class is overused. Strictly spoken, it 
would be only sensible to parse Haskell expressions with it, but actually 
it is mostly used for parsing user input like numbers. But why should a 
user enter a number in a Haskell compatible way or even the way, the 
current 'show' implementation does?

> 3. Change the documentation for read to point out the partiality and
> to recommend the above alternatives:

I prefer that.


More information about the Libraries mailing list