Use OCaml warning atttribute to disable warning 8:

2019-07-11 18:17发布

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.

1条回答
可以哭但决不认输i
2楼-- · 2019-07-11 18:45

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)= …)?

查看更多
登录 后发表回答