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

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

最佳答案

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

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

derive instance eqEntry :: Eq Entry

关于purescript - 如何为用户定义类型派生类型类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49671178/

相关文章:

purescript - purescript 中列表/数组中的类似记录类型

node.js - 错误: Cannot find module './pulp'

haskell - Haskell 和 PureScript 有什么区别?

purescript - 在 purescript-halogen 中创建后自动聚焦输入元素

haskell - 行为是共生体吗?

purescript - psci 中的未知模块 Data.List

purescript - 如何在Eff monad中使用guard

purescript - Purescript中Free Monad中的多态指令

google-cloud-firestore - 在 Purescript 中建模 Javascript 对象

purescript - 什么是 Eff 和 A​​ff?