[Haskell-cafe] Typeclasses, data, constructors

Joachim Fasting joachim.fasting at gmail.com
Wed Jul 27 16:09:30 CEST 2011


On 27 July 2011 10:45, Sergiy Nazarenko <nazarenko.sergiy at gmail.com> wrote:
> Hi everyone!
Hi,

> I have data declaration like this:
> I try to use typeclasses,
> class GetRow a where
>    hasID :: Int -> IO a
> instance GetRow MyTableOne where
>    hasID myid = return [(MyTableOne 1 "name")]
> instance GetRow MyTableTwo where
>    hasID myid = return [(MyTableTwo 1 "name" "path")]

The issue here seems to be that you are trying to define a class
instance for /constructors/, not types.

I don't really understand what it is you are trying to do, but perhaps you
could treat MyTableOne and MyTableTwo as distinct types.
Something like:

\begin{code}
data MyTableOne = MyTableOne Int String
data MyTableTwo = MyTableTwo Int String String

instance GetRow MyTableOne where ...

instance GetRow MyTableTwo where ...
\end{code}

You might even use a class for tables, so you can define arbitrary
table structures which all support a common interface (e.g., row fetching).

Hope this helps.

Regards,
Joachim



More information about the Haskell-Cafe mailing list