Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. newtype A = A Int
  2. newtype B = B Int
  3.  
  4. type Database = String
  5. type JSON = String
  6.  
  7. class ParseDatabase a where parse :: Database -> Maybe a
  8. instance ParseDatabase A where parse db = A <$> readMaybe s
  9. instance ParseDatabase B where parse db = B <$> readMaybe s
  10.  
  11. class ToJSON a where toJSON :: a -> JSON
  12. instance ToJSON A where toJSON (A n) = show n
  13. instance ToJSON B where toJSON (B n) = "{\"this one uses an object, not a bare number\": " ++ show n ++ "}"
  14.  
  15. database :: Database
  16. database = "3"
  17.  
  18. -- The thing you are asking for.
  19. foo :: Database -> Maybe JSON
  20. foo db = toJSON <$> parse db
  21.  
  22. -- Now I ask you: which of these two JSON objects do you expect to get from foo database, and why?
  23. -- 3
  24. -- {"this one uses an object, not a bare number": 3}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement