我目前正在构建一个项目,它允许不同类型的函数,具有不同数量的参数(但它们都是相同类型的).为了定义这些函数,我为每个函数使用了这个代码:
let {name} [a; b; {...}] =
{...}
Run Code Online (Sandbox Code Playgroud)
我的代码确保列表中的元素数量是正确的,如果不是这样的话,我会发现运行时错误.但我有一个警告,我想要隐藏,因为我知道这种模式匹配并非详尽无遗,而且我不希望看到警告不会警告我有关真正的错误我有制作.
另一方面:如果存在像Dafny(来自微软)这样的语言,那就是功能性的,我很乐意尝试那种语言.
编辑:如果没有办法做到这一点,请回答说明.在这种情况下,我将构建一个过滤掉这些警告的命令行工具.(但这可能会抹掉所有格式......)
你很有可能知道,你可以这样写:
let name = function
| [a; b; ... ] -> { ... }
| _ -> failwith "not possible"
Run Code Online (Sandbox Code Playgroud)
但是你可以(如果你坚持的话)通过在函数名和函数名[@warning "-8"]之间写入来禁用警告let.
$ 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
$
Run Code Online (Sandbox Code Playgroud)
更新
我发现了一种方法可以为功能的主体重新打开警告,尽管看起来很笨重:
$ 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
Run Code Online (Sandbox Code Playgroud)
如您所见,函数体中的匹配有警告,但函数参数没有警告.
总的来说,如果你期望人们阅读代码,这似乎有点太杂乱了.