I ran into something with Array.map2 that I don’t understand.
Consider this code:
type r1 = {
v1 : int
X : int
}
type r2 = {
v1 : int
Y : int
}
let a1 = [|{v1=1; X=1}; {v1=2; X=2}|] // val a1 : r1 [] ...
let a2 = [|{v1=100; Y=100}; {v1=200; Y=200}|] // val a2 : r2 [] ...
Array.map2 (fun x1 x2 -> (x1.X, x2.Y)) a1 a2 // works as expected
Array.map2 (fun x1 x2 -> (x1.v1, x2.v1)) a1 a2 // error FS0001: Type mismatch. Expecting a r2 [] but given a r1 []
Both records have a field v1. In the last line, I try to get a tuple of the values of v1, but from the different record types r1 and r2. It seems to throw an error when I try to select fields that have the same name on different records.
Somehow it jumps to the conclusion that x1 must be of type r2, why doesn’t the second line infer the type from the two parameters a1 and a2?
edit
This fixes it but I would expect to get the type right from the parameters.
Array.map2 (fun (x1:r1) x2 -> (x1.v1, x2.v1)) a1 a2 // works
The type checker works from left to right, so there isn’t enough information to resolve conflicts in the second example. Your first example is fine because
.Xand.Yare unique fields onr1andr2respectively.That said, in this case you could use piping to make types of
a1anda2available to the type checker prior accessing record fields:In general, you should use record patterns which contain unique fields to identify correct types:
or provide fully qualified field names: