我编写了一个函数,它将一个数组作为输入并返回一个大小相等的数组作为输出。例如:
myFunc [| "apple"; "orange"; "banana" |]
> val it : (string * string) [] =
[|("red", "sphere"); ("orange", "sphere"); ("yellow", "oblong")|]
现在我想通过 let 绑定来分配结果。例如:
let [|
( appleColor, appleShape );
( orangeColor, orangeShape );
( bananaColor, bananaShape )
|] =
myFunc [| "apple"; "orange"; "banana" |]
哪个效果很好...
> val orangeShape : string = "sphere"
> val orangeColor : string = "orange"
> val bananaShape : string = "oblong"
> val bananaColor : string = "yellow"
> val appleShape : string = "sphere"
> val appleColor : string = "red"
...除了它会产生警告:
warning FS0025: Incomplete pattern matches on this expression. For example, the value '[|_; _; _; _|]' may indicate a case not covered by the pattern(s).
警告的来源和原因已经介绍过,我只是在寻找一个简洁的解决方法。这个函数调用发生在我的函数顶部附近,我不喜欢将整个函数体放在一个匹配项中的想法:
let otherFunc =
match myFunc [| "apple"; "orange"; "banana" |] with
| [|
( appleColor, appleShape );
( orangeColor, orangeShape );
( bananaColor, bananaShape )
|] ->
// ... the rest of my function logic
| _ -> failwith "Something impossible just happened!"
那只是闻起来很糟糕。我也不喜欢忽略警告的想法——违背了我更好的判断。是否有其他选择可供我选择,还是我只需要完全找到不同的方法?