[Haskell-cafe] How to read safely?

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Wed Jun 24 10:26:40 EDT 2009


Magicloud Magiclouds <magicloud.magiclouds at gmail.com> wrote:

>   Read often throws runtime errors, which breaks the robust of the
> problem. How to deal with it? Without lost too much proformance (so
> reads is a no).
>   At least, if its error could be catched, that'd be better.

You might like to try using a real parser instead of read.  For
instance, the polyparse package provides module Text.Parse which
provides parser analogues for all of the Prelude instances of Read.  It
provides nice-ish error messages, or in the lazy variation, catchable
exceptions.

It also gives you a nice framework to write your own instances.
The DrIFT tool can automatically derive these instances for your own
datatypes, if you don't want to do it by hand.  (I expect it is easy to
teach the 'derive' tool to do it too.)

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list