如何为用户定义的类型派生类型类?

时间:2018-04-05 11:25:05

标签: purescript

如何为以下自定义类型派生相等类型类?

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的类型声明就在派生类型类的语句之上。

1 个答案:

答案 0 :(得分:3)

类型同义词不能包含类实例。为了拥有实例,您必须将Entry声明为datanewtype

newtype Entry = Entry 
    { firstName :: String 
    , lastName :: String 
    , address :: Address 
    } 

derive instance eqEntry :: Eq Entry