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#编译器指令。
首次禁用时,当前无法重新启用警告。
如果无法将元素数量解包到您的匹配项,则可能会(意外)导致运行时错误[a;b;c]
,因此,您可以使用对risc明确的完整匹配项:
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"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句