Created
October 4, 2019 22:46
-
-
Save dmwit/d9bca87865ac7bb941239f13ef03ffea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
newtype A = A Int | |
newtype B = B Int | |
type Database = String | |
type JSON = String | |
class ParseDatabase a where parse :: Database -> Maybe a | |
instance ParseDatabase A where parse db = A <$> readMaybe s | |
instance ParseDatabase B where parse db = B <$> readMaybe s | |
class ToJSON a where toJSON :: a -> JSON | |
instance ToJSON A where toJSON (A n) = show n | |
instance ToJSON B where toJSON (B n) = "{\"this one uses an object, not a bare number\": " ++ show n ++ "}" | |
database :: Database | |
database = "3" | |
-- The thing you are asking for. | |
foo :: Database -> Maybe JSON | |
foo db = toJSON <$> parse db | |
-- Now I ask you: which of these two JSON objects do you expect to get from foo database, and why? | |
-- 3 | |
-- {"this one uses an object, not a bare number": 3} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment