Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress a warning from OCaml in a specific line

Tags:

ocaml

I am currently building a project, that allows different types of functions, with a different amount of arguments (but all of them of the same type). To define those functions, I used this code for each of them:

let {name} [a; b; {...}] =
  {...}

My code around that ensures that the number of elements in the list is right, and I'd be fine with just a runtime error occurring if this isn't the case. But I am stuck with a warning, that I'd like to hide because I am aware of this pattern matching being non-exhaustive, and I don't want to see warnings that don't warn me in regards to real errors I have made.

On the other hand: If a language like Dafny (from Microsoft) exists, that is functional, I'd be happy to try that one.

Edit: If there isn't a way to do that, please answer stating exactly that. In that case, I'd build a command line tool that filters out those warnings. (But that would sadly erase all the formatting done...)

like image 490
CodenameLambda Avatar asked Nov 21 '25 08:11

CodenameLambda


1 Answers

As you most likely know, you can write something like this:

let name = function
    | [a; b; ... ] -> { ... }
    | _ -> failwith "not possible"

But you can (if you insist) disable the warning by writing [@warning "-8"] between the let and the function name.

$ cat warn.ml
let f [a; b] = a + b
$ ocamlc -c warn.ml
File "warn.ml", line 1, characters 6-20:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
(_::_::_::_|_::[]|[])

$ cat nowarn.ml
let [@warning "-8"] f [a; b] = a + b
$ ocamlc -c nowarn.ml
$ 

Update

I found a way to turn warnings back on for the body of the function, though it's clunky looking:

$ cat semiwarn.ml
let [@warning "-8"] f [a; b] =
    begin [@warning "+8"]
    let a' =
    match a with
        | 0 -> 14
        | 1 -> 15
    in
    a' + b
    end
$ ocamlc -c semiwarn.ml
File "semiwarn.ml", line 4, characters 4-52:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
2

As you can see, there is a warning for the match in the body of the function, but not for the function parameters.

On balance this seems a little too cluttered to be a good option if you expect people to be reading the code.

like image 155
Jeffrey Scofield Avatar answered Nov 23 '25 01:11

Jeffrey Scofield