How to define a record field as an array in f#?

耗尽温柔 提交于 2019-12-10 21:08:21

问题


I want to create a record field of type byte array with 8 elements but couldn't figure out the correct syntax.

I did something like:

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.


回答1:


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 |] }

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.



来源:https://stackoverflow.com/questions/26150233/how-to-define-a-record-field-as-an-array-in-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!