gpt4 book ai didi

list - 我可以在没有警告的情况下解压 F# 列表吗?

转载 作者:行者123 更新时间:2023-12-01 01:11:25 26 4
gpt4 key购买 nike

F# 使解包元组变得容易。解包列表也是可行的,但是编译器会发出警告:

let m = [1; 2; 3]
let [a; b; c] = m
// Incomplete pattern matches on this expression. For example, the value '[_;_;_;_]' may indicate a case not covered by the pattern(s)

有没有办法避免这个警告?

最佳答案

您可以使用 #nowarn 指令(在您的情况下为 #nowarn "25")禁用每个文件的警告,或者您可以在命令行上使用 --nowarn.

查看 F# Compiler Directives了解详情。

当第一次禁用警告时,目前无法重新启用警告。

如果元素的数量无法解包到 [a;b;c],您的匹配可能(意外)导致运行时错误,因此您可以使用明确的完全匹配风险:

let m = [1;2;3]
let (a,b,c) =
match m with
| [a;b;c] -> (a,b,c)
| _ -> failwith "Expected exactly three items in m"

关于list - 我可以在没有警告的情况下解压 F# 列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com