value restriction error details

201 views Asked by At
let empties = Array.create 100 []

Gives a value restriction error: error FS0030: Value restriction. The value 'empties' has been inferred to have generic type val empties : '_a list []. Either define 'empties' 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.

While this doesn't:

let makeArray () = Array.create 100 []

So what is the difference? I know there is a tradition for SO questions to include "what you have tried", but I don't even know what to try, it's a conceptual question...

1

There are 1 answers

0
s952163 On

The links in the comments should clear up most of your questions. But What is it that you want to achieve with this code? You want an array of 100 empty lists?

In fact makeArray () is not different from empties. If you execute it you will get the same error message:

error FS0030: Value restriction. The value 'it' has been inferred to have generic type val it : '_a list [] Either define 'it' 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.

This will create an array of 100 lists:
let mkArr2<'a> = Array.create<'a list> 100

Similar but with default 0 value (but I got rid of the [] list parameter):
let mkArr3<'a> = Array.zeroCreate<'a> 100

And finally an empty array:
let mkArr4<'a> = Array.empty<'a>

Or maybe something like this with traditional array initialization syntax:
let mkArr5<'a> = Array.init 100 (fun _ -> []:'a list)

For your specific example you can just add a generic type annotation. Use 'a list or 'a array if you want an array of lists/arrays.
let empties<'a> = Array.create<'a> 100