[Haskell-beginners] discover data constructor without pattern matching?

Andres Löh andres.loeh at googlemail.com
Fri Dec 2 09:16:05 CET 2011


Hi.

> Is it possible to discover the data constructor used to make a value without doing pattern matching?

You've already got an answer, but ...

> In my current small project I have a data type with a zillion constructors. These use record syntax so that I can access the payload without pattern matching. But I just discovered that it I want to print out the constructor name in my error handling function. Can I do this without, say, hacking around with a string from 'show'?

... to me, it sounds as if instead of:

        data X = A { x :: Int } | B { x :: Int } | C { x :: Int }

you might want:

        data X = MkX { x :: Int, sort :: Sort }
        data Sort = A | B | C deriving Show

Then you can trivially show the constructor, because it's just a value.

Cheers,
  Andres



More information about the Beginners mailing list