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}
Generally speaking,
Anytypes aren’t very useful. Consider: If you make a polymorphic list that can hold anything, what can you do with the types in the list? The answer, of course, is nothing – you have no guarantee that there is any operation common to these elements.What one will typically do is either:
Use GADTs to make a list that can contain elements of a specific typeclass, as in:
With this approach, you don’t know the concrete type of the elements, but you know they can be manipulated using elements of the
Footypeclass.Create a type to switch between specific concrete types contained in the list:
This can be combined with approach 1 to create a list that can hold elements that are of one of a fixed set of typeclasses.
In some cases, it can be helpful to build a list of manipulation functions:
This is useful for things like event notification systems. At the time of adding an element to the list, you bind it up in a function that performs whatever manipulation you’ll later want to do.
Use
Data.Dynamic(not recommended!) as a cheat. However, this provides no guarantee that a specific element can be manipulated at all, and so the above approaches should be preferred.