If the constructors of two variants share similar fields, how can I write a function that operates on the common ones without having to explicitly pattern match on the variants?
I’m looking to simplify the widerShape method in this snippet (the code is written in Reason):
type rect = {width: int, height: int};
type square = {width: int};
type shape =
| Rect rect
| Square square;
let widerShape shape :shape =>
switch shape {
| Rect r => Rect {...r, width: r.width + 100}
| Square s => Square {...s, width: s.width + 100}
};
type rect = {height: int};
type details =
| Rect rect
| Square;
type shape = {width: int, details};
let widerShape shape :shape => {...shape, width: shape.width + 100};
It depends which OCaml compiler you are using. If you are using 4.03.0 and above, I think you should be able to use this. Ping @SanderSpies If you are using Reason with BuckleScript, BuckleScript compiler still uses 4.02.3 so you have to wait until it upgrades to 4.04