In the routing section, the article says:
We can see that the RenderRoute class defines an associated data type providing the routes for our application.
What does associated data type mean? It means type families?
The Haskell standard data type Maybe is typically declared as: data Maybe a = Just a | Nothing. What this means is that the type Maybe has one type variable, represented by the a and two constructors Just and Nothing. (Note that Haskell requires type names and constructor names to begin with an uppercase letter).
Type Classes are a language mechanism in Haskell designed to support general overloading in a principled way. They address each of the concerns raised above. They provide concise types to describe overloaded functions, so there is no expo- nential blow-up in the number of versions of an overloaded function.
Indexed type families, or type families for short, are a Haskell extension supporting ad-hoc overloading of data types. Type families are parametric types that can be assigned specialized representations based on the type parameters they are instantiated with.
Haskell has three basic ways to declare a new type: The data declaration, which defines new data types. The type declaration for type synonyms, that is, alternative names for existing types. The newtype declaration, which defines new data types equivalent to existing ones.
Quoting code from the article:
instance RenderRoute HelloWorld where
data Route HelloWorld = HomeR
deriving (Show, Eq, Read)
renderRoute HomeR = ([], [])
As you can see Route
is an associated data type and yes, it means data families. Take a look at wiki example:
We define a type class whose instances are the types that we can use as keys in our generic maps:
class GMapKey k where data GMap k :: * -> * empty :: GMap k v lookup :: k -> GMap k v -> Maybe v insert :: k -> v -> GMap k v -> GMap k v
The interesting part is the associated data family declaration of the class. It gives a kind signature (here
* -> *
) for the associated data typeGMap k
- analogous to how methods receive a type signature in a class declaration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With