类型 class 实例头因使用类型(行类型)而无效
Type class instance head is invalid due to use of type (row type)
我需要为记录类型定义一个简单的类型类和实例:
class Codable param where
getCodec :: Codec param
type Obj = { id :: String }
instance Codable Obj where
getCodec = objCodec
尽管在为记录类型定义实例时出现错误:
Type class instance head is invalid due to use of type
( id :: string
)
All types appearing in instance declarations must be of the form T a_1 .. a_n, where each type a_i is of the same form, unless the type is fully determined by other type class arguments via functional dependencies.
但是这个消息对我来说听起来很神秘。有人可以详细说明吗,在这种情况下该怎么办?
PureScript 只是不支持行的实例,仅此而已。
如果您不知道,大括号语法是将 Record
构造函数应用于类型行的语法糖,即 { id :: String }
等同于 Record ( id :: String )
.
通常的解决方法是将您的记录包装在 newtype
:
newtype Obj = Obj { id :: String }
instance Codable Obj where ...
或者,如果您真的想为所有类型的记录提供编解码器(purescript-argonaut
所做的那种),您可以将行转换为 RowList
然后执行类型级别匹配上。这是一种高强度、高难度的技术,我现在不准备详细解释,但是here's an example, and here's another one. And here's the same thing used in purescript-argonaut
.
我需要为记录类型定义一个简单的类型类和实例:
class Codable param where
getCodec :: Codec param
type Obj = { id :: String }
instance Codable Obj where
getCodec = objCodec
尽管在为记录类型定义实例时出现错误:
Type class instance head is invalid due to use of type
( id :: string
)
All types appearing in instance declarations must be of the form T a_1 .. a_n, where each type a_i is of the same form, unless the type is fully determined by other type class arguments via functional dependencies.
但是这个消息对我来说听起来很神秘。有人可以详细说明吗,在这种情况下该怎么办?
PureScript 只是不支持行的实例,仅此而已。
如果您不知道,大括号语法是将 Record
构造函数应用于类型行的语法糖,即 { id :: String }
等同于 Record ( id :: String )
.
通常的解决方法是将您的记录包装在 newtype
:
newtype Obj = Obj { id :: String }
instance Codable Obj where ...
或者,如果您真的想为所有类型的记录提供编解码器(purescript-argonaut
所做的那种),您可以将行转换为 RowList
然后执行类型级别匹配上。这是一种高强度、高难度的技术,我现在不准备详细解释,但是here's an example, and here's another one. And here's the same thing used in purescript-argonaut
.