gpt4 book ai didi

OCaml 仿函数采用多态变体类型

转载 作者:行者123 更新时间:2023-12-04 14:58:04 25 4
gpt4 key购买 nike

试图编译

module F (M : sig
type t = [> `Foo ]
end) = struct
type t = [ M.t | `Bar ]
end

得到我
Error: A type variable is unbound in this type declaration.
In type [> `Foo ] as 'a the variable 'a is unbound

我究竟做错了什么?

最佳答案

type t = [> `Foo]无效,因为 [> `Foo]是一个开放类型并隐式包含一个类型变量。这个定义被拒绝,就像下面的类型定义被拒绝一样,因为 RHS 有一个在 LHS 中没有量化的类型变量:

type t = 'a list

你必须关闭它:
type t = [ `Foo ]

或量化类型变量:
type 'a t = [> `Foo] as 'a

这相当于
type 'a t = 'a constraint 'a = [> `Foo]

关于OCaml 仿函数采用多态变体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com