2

Why can't I compile a program with unused variables in Ocaml?

let foo a b = a + b

-- Error (warning 32 [unused-value-declaration]): unused value foo.

bitorhugo
  • 35
  • 6
  • 3
    You can. By default ocaml does not treat warnings as errors. You're using something else that enables this option. Probably `dune`. – glennsl Jul 28 '22 at 18:34

1 Answers1

4

You can disable the promotion of the warning to an error by customizing the flags in your dune file:

 (flags (:standard -warn-error "-unused-value-declaration"))

or disabling the promotion with an attribute in the file itself

[@@@warnerror "-unused-value-declaration"]

or for just the value:

let[@warnerror "-unused-value-declaration"] x = ()

(and you can use -w and @warning for disabling the warning itself rather than its promotion to an error.)

It is also possible to use a leading underscore to indicate the intent that a value is purposefully unused:

let _x = ()

Nevertheless, I find this warning generally useful to avoid dead code in the source code and I would not recommend to disable it.

octachron
  • 17,178
  • 2
  • 16
  • 23