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

Benjamin Edwards edwards.benj at gmail.com
Sun Jan 12 21:22:55 UTC 2014


If you just wanted the constructor name, show would have worked perfectly.

On Sun Jan 12 2014 at 15:45:28, Gareth Morgan <gmorgan1984 at gmail.com> wrote:

> 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/43d31e31/attachment-0001.html>


More information about the Beginners mailing list