Kvass
Kvass

Reputation: 8434

Explanations of OCaml Warnings

I've been reading through the list of OCaml warnings and am unsure what some of them mean (no examples are provided). Specifically, I'd like to understand:

Examples of code that trigger the following warnings (I think my interpretation of what each warning means is different from what it actually means because I am finding it difficult to produce cases that trigger the warnings that aren't outright language errors):

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

What an "ancestor variable" and an "extension constructor" are:

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

What these mean:

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

Upvotes: 3

Views: 1167

Answers (2)

octachron
octachron

Reputation: 18892

To complete the list:

  1. A wild card pattern can be used as argument of an argument-less constructor

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

Warning 28: wildcard pattern given as argument to a constant constructor

  1. This warning is raised when one names an inherited class without using it:

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

Warning 36: unused ancestor variable super.

  1. An extension construction is a constructor added to an extensible sum type like exn

    module M:sig end = struct
      type exn += Unused
     end
    

Warning 38: unused exception Unused

  1. With recent versions of OCaml, it is possible to avoid boxing record with only one field or variant types with one constructor. This unboxing currently requires an annotation

    type t = I of int [@@unboxed]
    

However, the default representation may change in the future. This change is transparent except for the FFI. Which means that external are paticularly brittle if they involve type without an annotation:

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

Warning 61: This primitive declaration uses type t, which is unannotated and unboxable. The representation of such types may change in future versions. You should annotate the declaration of t with [@@boxed] or [@@unboxed].

  1. Type constraints does not apply on GADT arguments when defining a variant type. For instance, in

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

Warning 62: Type constraints do not apply to GADT cases of variant types.

The warning explains that B [] is an error, whereas A[] is fine.

  1. This warning is an internal flambda warning, which warns that a value that flambda conjectured to be non-mutable was, in-fact, mutable. This warning should not be raised in normal circumstances.

Upvotes: 6

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66793

Here's an example for warning 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 = ()

Warning 6 is disabled by default. If you enable it, it's easy to produce:

$ 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

The rest are beyond what I can figure out without looking at compiler sources. Maybe an expert or two will show up who can give examples for them.

Upvotes: 4

Related Questions