According to the MSDN documentation on the for ... to loop in F#:
The type of the identifier is inferred from the type of the start and finish expressions. Types for these expressions must be 32-bit integers.
But, with the code below, I get the following compile-time error:
for bar = 0u to 5u do
let baz : uint32 = bar
()
error FS0001: This expression was expected to have type
int
but here has type
uint32
If I put the loop inside a sequence, though, it compiles without error:
let foo =
seq {
for bar = 0u to 5u do
let baz : uint32 = bar
yield baz
}
val foo : seq<uint32>
What's going on? Why does the for-loop infer uint32 in the second example but not the first?
I have an external library which takes an unsigned 32-bit integer as an index. I need to iterate from 0 to the length of the collection (also uint32) minus one. When I put this logic inside a sequence and yield each item, it compiles without any errors and runs just fine. But when I attempt to read all the items outside a sequence, the compiler bombs. I'm forced to perform type convers from uint32 to int and back again, which, in my opinion, has a very bad smell to it.