Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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