我一直在阅读OCaml警告列表,不确定其中的一些含义(未提供示例)。具体来说,我想了解:
触发以下警告的代码示例(我认为我对每种警告的含义的解释与实际含义不同,因为我发现很难产生触发警告的情况,而这些情况并非完全是语言错误):
5. Partially applied function: expression whose result has function type and is ignored.
6. Label omitted in function application.
28. Wildcard pattern given as argument to a constant constructor.
59. Assignment to non-mutable value
Run Code Online (Sandbox Code Playgroud)
什么是“祖先变量”和“扩展构造函数”:
36. Unused ancestor variable.
38. Unused extension constructor.
Run Code Online (Sandbox Code Playgroud)
这些是什么意思:
61. Unboxable type in primitive declaration
62. Type constraint on GADT type declaration
Run Code Online (Sandbox Code Playgroud)
要完成列表:
通配符模式可用作无参数构造函数的参数
type t = A
let f x = match x with A _ -> 0
Run Code Online (Sandbox Code Playgroud)警告28:通配符模式作为常量构造函数的参数给出
如果不使用继承的类命名继承的类,则会引发此警告:
class c = object end
class d = object
inherit c as super
end
Run Code Online (Sandbox Code Playgroud)警告36:未使用的祖先变量super。
扩展构造是添加到可扩展求和类型的构造函数,例如 exn
module M:sig end = struct
type exn += Unused
end
Run Code Online (Sandbox Code Playgroud)警告38:未使用的异常未使用
使用OCaml的最新版本,可以避免只使用一个构造函数的字段或变量类型装箱记录。此拆箱当前需要一个注释
type t = I of int [@@unboxed]
Run Code Online (Sandbox Code Playgroud)但是,默认表示形式将来可能会更改。除FFI之外,此更改是透明的。这意味着如果外部类型涉及没有注释的外部,则外部特别脆弱:
type t = I of int
external id: t -> t = "%identity"
Run Code Online (Sandbox Code Playgroud)
警告61:此原始声明使用类型t,该类型是未注释且不可装箱的。在将来的版本中,此类类型的表示形式可能会更改。您应使用[@@ boxed]或[@@ unboxed]注释t的声明。
在定义变体类型时,类型限制不适用于GADT参数。例如,在
type 'a t =
| A: 'a -> float t
| B of 'a
constraint 'a = float
Run Code Online (Sandbox Code Playgroud)警告62:类型约束不适用于变体类型的GADT情况。
警告说明这B []是错误,但是A[]可以。