Created
January 11, 2017 17:12
-
-
Save msgodf/1db2d4021479b37140bd640728ac20c1 to your computer and use it in GitHub Desktop.
An attempt to create a different instance of a type depending on the presence of a field in the foreign (JS) object
This file contains 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
data Sh = Sh1 {name :: String} | Sh2 {name :: String, age :: Int} | |
instance shIsForeign :: IsForeign Sh where | |
read x = do | |
name <- readProp "name" x | |
case (readProp "age" x) of | |
Left _ -> pure $ Sh1 {name:name} | |
Right age -> pure $ Sh2 {name: name, age: age} | |
frug :: Foreign -> String | |
frug f = case ((read f) :: (Either ForeignError Sh)) of | |
Left _ -> "neither" | |
Right (Sh1 {name:name}) -> "sh1" <> name | |
Right (Sh2 {name:name, age: age}) -> "sh2" <> name <> " " <> (show age) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment