Warning 37 Constructor -- is never used to build values

With the profile dev of Dune, I have just discover that some parts of my code generate this kind of warning:

Error (warning 37): constructor Nan is never used to build values.
(However, this constructor appears in patterns.)

for example :

type mixrampd_t =
  | Nan
  | Seconds of int

or

type gain_mode_t =
  | Off
  | Track
  | Album
  | Auto

Does this denote bad practices ?

1 Like

The warning should pose a useful question to you. Why do you need this Nan constructor if you’re never actually using it to construct values of mixrampd_t?

I created the mixrampd_t for this function:

let mixrampdelay client = function
  | Nan -> Client_lwt.send client "mixrampdelay nan"
  | Seconds (s) -> Client_lwt.send client (String.concat " " ["mixrampdelay";
                                                              string_of_int s])

Because the request I want to build has 2 cases, it accepts either “nan” or a number, so I though that the OCaml function should be like :

mixrampdelay client Nan
(* or *)
mixrampdelay client Seconds(3)

Are you using mli? Perhaps you forgot to expose it on the interface?

A simple example that explain the motivation behind warning 37 is

 module M: sig
  type t
  val f: t -> unit
end = struct
  type t = A 
  let f A = ()
end

Warning 37: constructor A is never used to build values.
(However, this constructor appears in patterns.)

The fact that the signature constraint of M makes the type t abstract (or hide it) is a crucial part: due to this signature outside of the definition of M it is not possible to access the variant constructor A . Consequently, M.A can never be constructed and the constructor could be removed without any trouble.

2 Likes

Yes, that is where you are pattern matching on Nan, but where are you constructing Nan values?

@bobbypriambodo, you are right, the example of @octachron illustrates totally my problem :). I made my type abstract in my mli.

@rgrinberg, its an interface for a client library. It is to the user to both use the Nan or Seconds constructors, that is why the compilator complains: there is no way to use those constructors with mixrampd_t abstract.

Thanks for your help.

Very old thread, but this helped me find a bug. I was using a same constructor twice, leaving the intended constructor unused. Thank you!