[Haskell-beginners] How to print the name of a variable

Stephen Tetley stephen.tetley at gmail.com
Sat Mar 20 07:03:35 EDT 2010


Hi Ozgur

Doesn't that associate names with types rather than names with values though:

instance HasName Int where
  name = "Int"


> printWithName (1::Int)
Int: 1

> printWithName (70::Int)
Int: 70

I think the same is achievable with Data.Typeable / Data.Data although
how to do it is somewhat buried...

For associating names and values you'd still need to do it 'by hand'
with something like

data Named a = Named String a
 deriving (Show)

or

type Named a = (String,a)

Best wishes

Stephen


More information about the Beginners mailing list