What is associated data type in haskell?
Asked Answered
L

1

8

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?

Lentigo answered 27/2, 2019 at 11:21 Comment(0)
N
10

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 type GMap k - analogous to how methods receive a type signature in a class declaration.

Niels answered 27/2, 2019 at 11:33 Comment(5)
I thought, that type families is defined with type instead of data like show in ocharles.org.uk/posts/2014-12-12-type-families.htmlLentigo
it depends what you want: using existing type (type) or creating a new one (data)Niels
Look at the yesodweb.com/book/haskell in the type families, there is a definition of type Content a and what is it?Lentigo
@zero_coding The TypeFamilies extension includes both type families and data families. They are very similar conceptually, with the difference between them being the same as the difference between (non-family) type aliases and (non-family) data types.Emery
what are the differences between type families and data families?Lentigo

© 2022 - 2024 — McMap. All rights reserved.