在OCaml中,可以按如下方式在列表中定义变量x和y,但您会得到警告:
let [x; y] = [2; 3];;
Characters 4-10:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(_::_::_::_|_::[]|[])(在我的实际代码中,我有一个返回值列表的函数。有时我知道它只会返回两个值。)
在使用match或function时,我知道如何通过添加一个catch案例来抑制警告,在下面列出的问题的答案中对此进行了解释。当我使用上面的列表定义时,抑制警告的最好方法是什么?
发布于 2017-09-01 18:23:27
使用所有的情况,如在
let (x,y) = match [2;3] with
| [x;y] -> (x,y)
| _ -> assert false (* how could this list not have exactly 2 elements?*)这可能不是个好主意,因为它给了你空间来评论为什么你只期望第一个模式。尽管如此,如果您确实坚持使用let,则可以使用属性暂时禁用警告,如下所示:
[@@@ warning "-8"]
(* My list is guaranteed to have two elements. disable warning for a while. *)
let [x;y] = [2;3];;
[@@@ warning "+8"]https://stackoverflow.com/questions/46005684
复制相似问题