Search code examples
compiler-errorsocamlunused-variablesunused-functions

Unused let expression in Ocaml


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.


Solution

  • 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.