
Hi everyone! I have data declaration like this: data MyTable = MyTableOne Int String | MyTableTwo Int String String and function that insert new row in tables: newRow :: MyTable -> IO Int newRow (MyTableOne fld1 fld2 ) = ..... newRow (MyTableTwo fld1 fld2 fld3 ) = ...... That work perfectly, I enjoy it. But now I want to get rows, and I don't know what to do :( I want func something like this: getRow :: Int -> [MyTable] .... - what should I write here ? Possibly use this function: getRow 23 :: MyTableTwo (like haskell function Read for example: read "7" :: Int) 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")] But I have error: Not in scope: type constructor or class `MyTableOne' make: *** [Foo.so] Error 1 Not in scope: type constructor or class `MyTableTwo' make: *** [Foo.so] Error 1 I ask you to suggest me right way in my efforts Cheers!

On 27 July 2011 10:45, Sergiy Nazarenko
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
participants (2)
-
Joachim Fasting
-
Sergiy Nazarenko