Discussion: adding displayException to Exception class

Jeremy Shaw jeremy at n-heptane.com
Wed Oct 29 20:19:05 UTC 2014


On Wed, Oct 29, 2014 at 10:20 AM, Christopher Done <chrisdone at gmail.com> wrote:
> Rather, I
> want to know what the value is. So I need the constructor and any
> fields in it. I think it’s essential for debugging, and I feel spoiled
> in Haskell that most types have a reasonable Show instance that shows
> the structure of a value for me.

> I think the case of “being able to copy/paste it as source code” is a
> little bit out of place given things like fromList [(1,'a')] for a
> Map Int Char and Handle {<1>} (or whatever it was for the Handle
> type). The Handle’s Show instance is, to my mind, a perfectly
> legitimate instance for telling you what this value is. It contains an
> fd and it prints it. It has no Read instance, there’s no pretense
> of being “serializable” or “copy/pastable”.

It is definitely the case that not all useful values can be shown as
valid Haskell code. But in the cases where they can -- I think it is
useful even if you never actually copy & paste. If the value is shown
as valid Haskell then you truly have all the information you could
possibly need about that value. And you get it in a syntax that you
are already familiar with -- so there is no need to learn another way
of reading things.

I'm curious about your, fromList [(1, 'a')], example. That seems
exactly like a case where the Show instance is providing you with
valid Haskell code to recreate the value aside from issue that
fromList could be ambiguous.

If I had to create some rules for Show it would probably involve:

 1. if it is possible to write a Read instance, then the Show instance
should create valid Haskell code
 2. if a Read instance is not possible, then the Show instance should
come as close to valid Haskell as possible while yielding the maximum
amount of information available.

It seems like both Map and Handle would meet those requires. Map has
Read/Show instances and the String format is valid Haskell. For
Handle, a Read instance is not possible, but it does give you as much
information as possible.

I believe 'deriving (Read, Show)' naturally meets requirement #1 as
long as any Read/Show instances it depends on also meet requirement #1
?

- jeremy


More information about the Libraries mailing list