I have some very basic code which works:
let randomNumberGenerator = System.Random.Shared
let randomFraction = (fun () -> randomNumberGenerator.NextDouble())
let shuffle (randomNumber : unit -> float) (data: 'a list) = data |> List.sortBy (fun _ -> randomNumber())
let shuffler data = shuffle randomFraction data
[ 1; 2; 3; 4; 5; 6 ] |> shuffler
If I remove the ‘data’ parameter from the end of the shuffler function definition, e.g. let shuffler data = shuffle randomFraction
– I don’t get a compiler error but I get this when I run the code via the FSI:
Scratchpad3.fsx(14,1): error FS0030: Value restriction. The value ‘it’ has been inferred to have generic type
val it: ('_a list → '_a list)
Either make the arguments to ‘it’ explicit or, if you do not intend for it to be generic, add a type annotation.
If I then remove the ‘data’ from the shuffler parameters, e.g. let shuffler = shuffle randomFraction
- the code works again.
However, if I then comment out the last line, where the shuffler function is being used, I get a compiler error:
Severity Code Description Project File Line Suppression State
Error FS0030 Value restriction. The value ‘shuffler’ has been inferred to have generic type
val shuffler: ('_a list → '_a list)
Either make the arguments to ‘shuffler’ explicit or, if you do not intend for it to be generic, add a type annotation. F# Miscellaneous Files \Scratchpad3.fsx 12 Active
This seems odd to me but should I be expecting it?
There’s probably a perfectly reasonable explanation but I don’t understand why the shuffler function should become ‘invalid’ when it’s not being ‘used’.