Skip to content

Instantly share code, notes, and snippets.

@matfournier
Created December 26, 2019 06:08
Show Gist options
  • Save matfournier/716047dcf9523cb56fa31b366c5e9550 to your computer and use it in GitHub Desktop.
Save matfournier/716047dcf9523cb56fa31b366c5e9550 to your computer and use it in GitHub Desktop.
stupid adt example
module Scratch where
data Shape = Circle { radius :: Int }
| Rectangle {length :: Int, width :: Int}
| Triangle {length :: Int, height :: Int}
deriving (Show, Eq)
-- how would you fromJSON shape assuming there
-- was no tag on the json
-- e.g.
-- {"radius": 1 } <- a circle
-- {"length": 1, "width": 1} <- a rectangle
-- {"length": 1, "height": 2} <- a triangle
-- none of the JSON are tagged with what shape
-- they are
-- e..g there is no {"tag": "Circle", "radius": 1}
-- I could have a complicated ToJSON method
-- and start looking for keys?
-- does making an intermediate wire format type
-- for Shape that holds all possible shapes work?
data UrShape = UrShape {
radius_ :: Maybe Int
, length_ :: Maybe Int
, width_ :: Maybe Int
, height_ :: Maybe Int
}
instance FromJSON UrShape ...
-- and a bunch of
asCircle :: UrShape -> Maybe Circle
asRectnalge :: UrShape -> Maybe Rectangle
asTriangle :: UrShape -> MaybeTriangle
-- and some parser that is uhh
-- asCircle <|> asRectangle <|> asTriangle
--?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment