Fix encoder for data type with phantom types
chshersh opened this issue · comments
Since it's not possible to get information about type variables from Generic
instances, we need to write Elm
instances for types with phantom type variables manually like this:
newtype Id a = Id { unId :: String }
instance Elm (Id a) where
toElmDefinition _ = DefType $ ElmType "Id" ["a"] $ ElmConstructor "Id" [RefPrim ElmString] :| []
Unfortunately, because of that, encoder for Id
is not what we want:
ghci> T.putStrLn $ prettyShowEncoder $ toElmDefinition $ Proxy @(Id Int)
encodeId : Ida -> Value
encodeId x = E.object <| case x of
Id x1 -> [(tag, "Id"), ("contents", E.list [E.string x1])]
Though, when we use Id
, everything is great!
λ: data User = User { userId :: Id User } deriving (Generic) deriving anyclass (Elm)
λ: T.putStrLn $ prettyShowDefinition $ toElmDefinition $ Proxy @User
type alias User =
{ id : Id User
}
λ: T.putStrLn $ prettyShowEncoder $ toElmDefinition $ Proxy @User
encodeUser : User -> Value
encodeUser x = E.object
[ ("id", encodeId User x.id)
]