[Haskell-beginners] Getting a variables type as a string at runtime

Gareth Morgan gmorgan1984 at gmail.com
Sun Jan 12 15:45:17 UTC 2014

Data.Typeable with Data.Data worked. Thank you.

Using Typeable alone was only giving me the type of the whole type.
Data.Data had the toConstr function needed to give me the particular
constructor name.

For reference if anyone is interested in the solution:

import Data.Typeable
import Data.Data

data A  = A | B | C deriving (Typeable, Data)

typeName :: A -> String
typeName = show . toConstr

I needed to add DeriveDataTypeable extension to my cabal file

On Sun, Jan 12, 2014 at 2:53 PM, Brandon Allbery <allbery.b at gmail.com>wrote:

> On Sun, Jan 12, 2014 at 9:47 AM, Gareth Morgan <gmorgan1984 at gmail.com>wrote:
>> Won't show include all the components? I wanted to include only the type
>> name.
> Types only exist at compile time normally. But if you add a Typeable
> constraint (see Data.Typeable and the DeriveDataTypeable extension) you can
> get type names at runtime.
> --
> brandon s allbery kf8nh                               sine nomine
> associates
> allbery.b at gmail.com
> ballbery at sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140112/e8db0d99/attachment.html>

More information about the Beginners mailing list