OCaml警告说明

时间:2019-03-23 18:07:06

标签: ocaml

我一直在阅读list of OCaml warnings,不确定其中的含义(未提供示例)。具体来说,我想了解:

触发以下警告的代码示例(我认为我对每种警告的含义的解释与实际含义不同,因为我发现很难产生触发并非完全是语言错误的警告的案例):

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

什么是“祖先变量”和“扩展构造函数”?

36. Unused ancestor variable.
38. Unused extension constructor.

这些是什么意思:

61. Unboxable type in primitive declaration
62. Type constraint on GADT type declaration

2 个答案:

答案 0 :(得分:5)

要完成列表:

  1. 通配符模式可用作无参数构造函数的参数

    type t = A
    let f x = match x with A _ -> 0
    
  

警告28:通配符模式作为常量构造函数的参数

  1. 如果不使用继承的类而命名继承的类,则会发出此警告:

    class c = object end
    class d = object
       inherit c as super
    end
    
  

警告36:未使用的祖先变量super。

  1. 扩展构造是添加到诸如exn

    之类的可扩展求和类型中的构造函数。
    module M:sig end = struct
      type exn += Unused
     end
    
  

警告38:未使用的异常未使用

  1. 使用OCaml的最新版本,可以避免仅将一个字段或变量类型与一个构造函数装箱。此拆箱操作目前需要注释

    type t = I of int [@@unboxed]
    

但是,默认表示形式将来可能会更改。 除FFI之外,此更改是透明的。这意味着如果外部类型涉及没有注释的外部,则外部特别脆弱:

  type t = I of int
  external id: t -> t = "%identity"
  

警告61:此原始声明使用类型t,该类型未注释且   无法装箱。此类类型的表示将来可能会更改   版本。您应该使用[@@ boxed]注释t的声明   或[@@ unboxed]。

    在定义变体类型时,
  1. 类型约束不适用于GADT参数。例如,在

    type 'a t = 
    | A: 'a -> float t 
    | B of 'a
    constraint 'a = float
    
  

警告62:类型约束不适用于变体类型的GADT情况。

警告说明B []是错误,而A[]很好。

  1. 此警告是内部flambda警告,它警告flambda推测为不可更改的值实际上是可变的。在正常情况下不应发出此警告。

答案 1 :(得分:4)

以下是警告5的示例:

# let f a b = a + b;;
val f : int -> int -> int = <fun>
# ignore (f 3);;
Warning 5: this function application is partial,
maybe some arguments are missing.
- : unit = ()

默认情况下,警告6是禁用的。如果启用它,很容易产生:

$ rlwrap ocaml -w +6
        OCaml version 4.06.1
# let f ~a = a * 10;;
val f : a:int -> int = <fun>
# f 3;;
Warning 6: label a was omitted in the application of this function.
- : int = 30

其余内容超出了我在不查看编译器源代码的情况下可以发现的范围。也许会出现一两个专家,谁能为他们提供示例。