如何定义一个记录字段作为F#数组? [英] How to define a record field as an array in f#?
问题描述
我要创建类型的字节数组的记录字段有8个元素,但无法找出正确的语法。
I want to create a record field of type byte array with 8 elements but couldn't figure out the correct syntax.
我不喜欢的东西:
let dataRecord = {
id : int
data : byte array
}
let dataValues : byte array = Array.zeroCreate 8
let myArray = { id = 0; data = dataValues }
是否可以在记录的定义做了什么?怎么样?
Can it be done in the record definition? How?
我的例子,上面似乎工作,但我不知道这是否是安全的或最好的或最正确的方法。
My example, above, seemed to work but I don't know if it is safe or the best or most correct way.
推荐答案
有没有错,你现在在做什么(比其他使用让你的类型定义
而不是键入
),所以它不是完全清楚,我你问什么。也许这样的事情?
There's nothing wrong with what you're currently doing (other than that your type definition is using let
instead of type
), so it's not completely clear to me what you're asking for. Maybe something like this?
type dataRecord = {
id : int
data : byte array
}
let myRecord = { id = 0; data = [| for i in 1 .. 8 -> 0uy |] }
您也可以只使用 {ID = 0;数据= Array.zeroCreate 8}
如果你喜欢 - 数组文本往往是有点更容易阅读,但 zeroCreate
是,如果可能更有效你创建的大阵。
You could also just use { id = 0; data = Array.zeroCreate 8 }
if you'd like - array literals are often a bit easier to read, but zeroCreate
is probably more efficient if you're creating big arrays.
这篇关于如何定义一个记录字段作为F#数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!