Created
December 26, 2019 06:08
-
-
Save matfournier/716047dcf9523cb56fa31b366c5e9550 to your computer and use it in GitHub Desktop.
stupid adt example
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
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