Reputation: 21737
type A =
{
...
id: int;
...
}
I wish i could do this
let Add (x:A) (y:A) =
match x,y with
| {x.id=0,y.id=1} -> ...
And is there any trick to define the function if i don't care about the order of x
and y
(so that the function is symmetric) also i don't mind whether the parameter is a tuple (x,y)
or a higher order function x,y
Upvotes: 3
Views: 862
Reputation: 4724
Another syntax for this is:
let add x y =
match x, y with
| {id = 0}, {id = 1} | {id = 1}, {id = 0} -> ..
| _ -> ..
See Record Pattern section at http://msdn.microsoft.com/en-us/library/dd547125.aspx
Upvotes: 12
Reputation: 41290
let add (x: A) (y: A) =
match x.id, y.id with
| 0, 1 | 1, 0 -> (* do some thing *)
| _ -> (* do some thing else *)
If you only care about a field, do pattern matching directly on it. And you can use Or pattern to have a symmetric function.
Upvotes: 4