使用OCaml警告属性禁用警告8:不完全匹配 [英] Use OCaml warning attribute to disable warning 8: unexhaustive match

查看:79
本文介绍了使用OCaml警告属性禁用警告8:不完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写类似于以下内容的代码:

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

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

我只想针对模式 [a;b] ,而不是身体或容身之外的任何物品.我尝试将警告属性设置为禁用警告,但以下任何一项均无效:

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.我并不是真正在编写此代码,而是在尝试使用自定义PPX预处理程序.因此,可以使用一个复杂但可行的示例.

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.

推荐答案

使用 [@ warning"..."] [@@ warning"..."] 在本地禁用警告code> 4.06.0之前的OCaml版本不完全支持.对于这种版本,一种可能是使用封闭的 [@@@ warning"] 属性:

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.

如果您正在生成代码并完全了解列表的大小,那么另一个选择可能是使用元组进行绑定(aka let(a,b)=…)?

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

这篇关于使用OCaml警告属性禁用警告8:不完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆