Alex Rivera | Logout

Is there a type 'Any' in haskell?

Asked 2011-06-25T17:37:20.623
48

Say, I want to define a record Attribute like this:

data Attribute = Attribute {name :: String, value :: Any}

This is not valid haskell code of course. But is there a type 'Any' which basically say any type will do? Or is to use type variable the only way?

data Attribute a = Attribute {name :: String, value :: a}

Edit
Report

1 Answer

15

This sounds like a pretty basic question, so I'm going to give an even more basic answer than anybody else. Here's what is almost always the right solution:

data Attribute a = Attribute { name :: String, value :: a }

Then, if you want an attribute that wraps an Int, that attribute would have type Attribute Int, or an attribute that wraps a Bool would have type Attribute Bool, etc. You can create these attributes with values of any type; for example, we can write

testAttr = Attribute { name = "this is only a test", value = Node 3 [] }

to create a value of type Attribute (Tree Int).

answered 2011-06-25T18:59:21.757

Your Answer