在 F# 中声明返回 2DArray 的函数的语法
我需要在 F# 中声明一个函数,它接受 2 个参数(行、列)并返回 Option 的二维数组(将所有元素初始化为无),但我不知道正确的语法。我尝试过类似的操作:
type T =
{
....//my type
}
let create2DArrayOfT (row : int , col: int) Array2D<Option<T>> = Array2D.init<Option<T>> 10 10 (fun -> None)
上面的签名在指定返回类型时是错误的。 所以我有两个问题:
- 将返回类型指定为二维数组的正确签名是什么?
- 我尝试对数组的元素使用 Option ,因为我希望它允许某些位置为空。这合理吗?
I need to declare a function in F# that takes 2 parameters (row, col) and return an 2-dimensional array of Option (intitializing all elements to be none), but I don't know the right syntax. I've tried something like:
type T =
{
....//my type
}
let create2DArrayOfT (row : int , col: int) Array2D<Option<T>> = Array2D.init<Option<T>> 10 10 (fun -> None)
the signature above is wrong in specifing the return type.
So I have 2 questions:
- What's the right signature for specifying the return type as a 2-dimensional array?
- I've tought to use Option for the elements of my array because I want it to allow some locations to be empty. Is this reasonable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
二维数组的类型在 F# 中写作
'a [,]
。下次,您可以使用 F# 交互方式通过查看 Array2D 模块中的函数类型来找到这一点:The type of a 2D array is written
'a [,]
in F#. Next time, you can find this out using F# interactive by looking at the type of the functions in theArray2D
module:您不需要指定返回类型,因为它将通过类型推断推导出来。
只需使用:
更新:
如果您想指定返回类型,请使用:
You don't need to specify return type as that will be deduced by type inference.
Just use:
UPDATE:
If you want specify return type use:
注释省略
用法:
anotation omission
usage: