Vladimir Keleshev
Vladimir Keleshev

Reputation: 14245

Use OCaml warning attribute to disable warning 8: unexhaustive match

I'm trying to write code similar to the following:

let [a; b] =
  (* body *) 
  [1; 2]

And I want to disable warning # 8 just for the pattern [a; b] and not for the body or for anything outside of the let. I've tried to put the warning attribute to disable the warning, but none of the below work:

let[@warning "-8"] [a[@warning "-8"];b[@warning "-8"]] [@warning "-8"] =
  [1;2][@warning "-8"]
[@@ocaml.warning "-8"]

P.S. I'm not really writing this code, but am experimenting with a custom PPX preprocessor. So a convoluted but working example is acceptable.

Upvotes: 5

Views: 2084

Answers (1)

octachron
octachron

Reputation: 18892

Local disabling of warnings with [@warning "…"] and [@@warning "…"]is not well supported for OCaml version anterior to 4.06.0 . For such version, one possibility might be to use enclosing[@@@warning ""] attribute:

[@@@warning "-8"]
let [a;b] = [1;2]
[@@@warning "+8"]

but this also deactivate the warning inside the body.

If you are generating the code and know statiscally the size of the list, another option might be to use a tuple for the binding (aka let (a,b)= …)?

Upvotes: 5

Related Questions