是否可以派生递归数据类型的泛型实例?

问题描述 投票:0回答:1

我正在使用某些类型和PureScript编译器。这些是我创建的类型(通常从purescript-dsl-example中被盗):

newtype User = User
  { id :: Int
  , name :: String
  }

data Command a = Add User a
               | Remove Int a
               | ChangeName Int String a

此类型检查并编译。然后,考虑到能够将这些类型序列化为JSON可能有用,我安装了purescript-foreign-generic并添加了以下内容:

derive instance genericCommand :: Generic Command _

作为显示实例的第一步。

然后类型检查器抛出此错误:

Error found:
in module Main
at src/Main.purs:33:43 - 33:50 (line 33, column 43 - line 33, column 50)

  Could not match kind

    Type

  with kind

    Type -> Type


while checking the kind of Generic Command (Sum (Constructor "Add" (Product ... ...)) (Sum (Constructor "Remove" ...) (Constructor "ChangeName" ...)))
in value declaration genericCommand

是否有从我的类型派生通用实例的方法?如果没有,是否可以手动编写通用实例?我不知道那会带来什么,所以可以接受,我不确定最后一个问题是否有意义。

purescript
1个回答
0
投票

当然,可以为您的类型派生Generic,只是您使用的语法不正确。应该是这样的:

derive instance genericCommand :: Generic (Command a) _

[当您写入Generic Command _时,Command部分(它是Generic的第一个参数)具有类型Type -> Type-也就是说,如果将Command应用于Type,则得到一个Type。因此,例如,Command Int将具有类型Type。但是Command本身具有种类Type -> Type

但是Generic类期望类型为Type而不是Type -> Type的第一个参数,这就是错误消息告诉您的内容。

© www.soinside.com 2019 - 2024. All rights reserved.