Skip to content

Instantly share code, notes, and snippets.

@dmwit
Created October 4, 2019 22:46
Show Gist options
  • Save dmwit/d9bca87865ac7bb941239f13ef03ffea to your computer and use it in GitHub Desktop.
Save dmwit/d9bca87865ac7bb941239f13ef03ffea to your computer and use it in GitHub Desktop.
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