views:

499

answers:

3

Consider the following code fragment in VS2010 Beta 1:

let array = Array2D.zeroCreate 1000 500

This produces an error, namely:

 error FS0030: Value restriction. The value 'array' has been inferred to have 
generic type val array : '_a [,]
Either define 'array' as a simple data term, make it a function with explicit 
arguments or, if you do not intend for it to be generic, add a type annotation.

Can I explicitly set the type (in my case a grid of string)?

+6  A: 

You can explicitly specify the type like this:

let array : string [,] = Array2D.zeroCreate 1000 500

For further information on the value restriction you might want to take a look at this F#-Wiki page.

Johan Kullbom
+2  A: 

You can also use init to create an array though it might be slower.

let array = Array2D.init 1000 500 (fun _ _ -> "")

Zeroing out an array is usually not seen in functional programming. It's much more common to pass an initilization function to init and just create the array with the values you want.

gradbot
+1  A: 

To create a 2-dimensional array containing empty strings:

let array = Array2D.create 1000 500 ""
Joh