如何为以下自定义类型派生相等类型类?
type Address =
{ street :: String
, city :: String
, state :: String
}
type Entry =
{ firstName :: String
, lastName :: String
, address :: Address
}
derive instance eqEntry :: Eq Entry
编译时收到以下错误:
Cannot derive a type class instance, because the type declaration for Entry could not be found.
这让我感到困惑,因为我可以看到Entry的类型声明就在派生类型类的语句之上。
答案 0 :(得分:3)
类型同义词不能包含类实例。为了拥有实例,您必须将Entry
声明为data
或newtype
:
newtype Entry = Entry
{ firstName :: String
, lastName :: String
, address :: Address
}
derive instance eqEntry :: Eq Entry