【发布时间】:2014-03-23 10:50:51
【问题描述】:
我正在尝试将带有选项 int 元素的 Array2D optionArr 转换为带有 int 元素的 Array2D arr:
let arr =
optionArr
|> Array2D.map (fun x ->
match Option.toArray x with
| [| |] -> -1
| [| v |] -> v)
然而,Visual Studio 2013 用红色强调了从 Array2D.map ... 到 ... -> v) 的所有内容,并表示:
Type mismatch. Expecting a
int [,] option -> 'a
but given a
'b [,] -> 'c [,]
The type 'int [,] option' does not match the type ''a [,]'
我一直在尝试“修复”我的代码,但我不知道我做错了什么,也不知道上述错误消息暗示了什么。
编辑
我申请了Reed Copsey's answer(它本身使用Marcin's approach),但当我意识到该消息明确指出Array2Darr 的类型为int [,] option 而不是int option [,] 时,仍然收到上述错误消息.应用相同的逻辑,我更正的代码如下:
let arr = defaultArg optionArr (Array2D.zeroCreate 0 0)
defaultArg 似乎对于将 Option 值视为“正常”值非常有用。
【问题讨论】:
-
如何声明
optionArr?真的是二维数组还是锯齿数组? -
不是锯齿状数组,而是二维数组。有关声明,请参阅 Tomas Petricek 对 stackoverflow.com/questions/21919785/… 的回答。