Unused let expression in Ocaml

719 Views Asked by At

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.

1

There are 1 best solutions below

0
octachron On BEST ANSWER

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.