Skip to content

Instantly share code, notes, and snippets.

@msgodf
Created January 11, 2017 17:12
Show Gist options
  • Save msgodf/1db2d4021479b37140bd640728ac20c1 to your computer and use it in GitHub Desktop.
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
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