关于Ocaml中参数推断的一个问题
下面是一些使用Ocaml和Yojson(一个支持Ocaml的Json库)的Ocaml代码 然后我展示了Yojson类型的定义关于Ocaml中参数推断的一个问题,ocaml,Ocaml,下面是一些使用Ocaml和Yojson(一个支持Ocaml的Json库)的Ocaml代码 然后我展示了Yojson类型的定义 type json = [ `Assoc of (string * json) list | `Bool of bool | `Float of float | `Int of int | `List of json list | `Null | `String of string ]
type json = [ `Assoc of (string * json) list
| `Bool of bool
| `Float of float
| `Int of int
| `List of json list
| `Null
| `String of string ]
goal\u feats\u json
和args\u feats\u json
的类型都是json
。
goal\u feats\u json
的具体类型是[
。tac_str
的类型是string
。
args\u feats\u json
的具体类型是[>Assoc of(string*[>Int of'b | string of'c])list
。
但是当我编译文件时,出现了以下错误
Error: This expression has type [< `Int of 'a ] list but an expression was expected of type
[> `Assoc of (string * [> `Int of 'b | `String of 'c ]) list ] list*
错误:此表达式的类型为[<`Int of'a]列表,但表达式的类型应为
[>`Assoc of(string*[>`Int of'b |`string of'c])列表]列表*
似乎Ocaml将
goal\u feats\u json
和args\u feats\u json
都视为[
。我不知道为什么会发生这样的错误。如果有人能给我一些帮助,我将不胜感激。您显示的代码片段中没有错误,因此不可能知道根本问题在哪里。但是,您的类型错误可以通过显式强制来解决。比如说,
让f(x:[<`Int of Int])=(x:>json)
很可能,您使用了
goal\u fets\u json
作为函数的参数,该函数仅适用于类型的值[非常感谢!我使用goal\u fets\u json
作为函数的参数,该函数仅适用于[Int of.]。你能帮我解释一下为什么我的代码会限制它的类型吗?因为如果你的代码只处理[Int of uz]
类型的值,那么这样做是正确的。我不确定你的问题是什么?
Error: This expression has type [< `Int of 'a ] list but an expression was expected of type
[> `Assoc of (string * [> `Int of 'b | `String of 'c ]) list ] list*