Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a byte array with dynamic size in F#

Tags:

arrays

.net

f#

In C# and Java a byte array can be created like this

byte[] b = new byte[x];

where x denotes the size of the array. What I want to do is to do the same thing in F#. I have searched for how to do it and looked for it in the documentation. I think that I'm probably using the wrong search terms because I can't find out how.

What I've found so far is that Array.create can be used like this:

let b = Array.create x ( new Byte() )

Is there another way to do it which is more similiar to the way it can be done in C# and Java?

like image 656
John Tall Avatar asked Jun 16 '12 13:06

John Tall


1 Answers

A closest F# analog would be Array.zeroCreate:

let b: byte [] = Array.zeroCreate x

Instead of implicit array elements initialization to 0 bytes on Java and C# platforms F# makes the initial value of array elements obvious.

As to dynamic size of b in F# it is defined once by x value at the allocation and cannot be changed later by changing x, similarly to C#/Java,.

like image 85
Gene Belitski Avatar answered Sep 20 '22 19:09

Gene Belitski