Advertisement
Nevoic

Error with typeclasses

Oct 2nd, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE GADTs, RecordWildCards #-}
  2. module BreakingTypeClasses where
  3.  
  4. data ItemRepo a where
  5.   ItemRepo :: Item a =>
  6.     { getItem :: Int -> IO (Maybe a)
  7.     , saveItem :: a -> IO ()
  8.     } -> ItemRepo a
  9.  
  10. class Item a where
  11.   id :: a -> Int
  12.   display :: a -> String
  13.  
  14. data User = User
  15.   { userID :: Int
  16.   , name :: String
  17.   , favoriteNum :: Int
  18.   }
  19.  
  20. data Task = Task
  21.   { taskID :: Int
  22.   , description :: String
  23.   }
  24.  
  25. instance Item User where
  26.   id = userID
  27.   display User {..} = name ++ "\n\tFavorite Number: " ++ num
  28.     where num = show favoriteNum
  29.  
  30. instance Item Task where
  31.   id = taskID
  32.   display = description
  33.  
  34. getItemRoute :: Item a => ItemRepo a -> IO ()
  35. getItemRoute repo = do
  36.   id <- param "id"
  37.   item <- getItem repo id
  38.   json $ display item
  39.  
  40. param :: String -> IO a
  41. param = undefined
  42.  
  43. json :: String -> IO ()
  44. json = undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement