1
votes

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
1

1 Answers

5
votes

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 .X and .Y are unique fields on r1 and r2 respectively.

That said, in this case you could use piping to make types of a1 and a2 available to the type checker prior accessing record fields:

(a1, a2) ||> Array.map2 (fun x1 x2 -> (x1.v1, x2.v1))

In general, you should use record patterns which contain unique fields to identify correct types:

Array.map2 (fun {v1 = a; X = _} {v1 = b; Y = _} -> (a, b)) a1 a2

or provide fully qualified field names:

Array.map2 (fun {r1.v1 = a} {r2.v1 = b} -> (a, b)) a1 a2