# How to access identical fields from variant constructors without pattern matching?

**URL:** <https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300>\
**Category:** Learning\
**Created:** [May 30, 2017, 6:16pm UTC](https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300 "2017-05-30T18:16:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasim](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jasim/32/168_2.png) [@jasim](https://discuss.ocaml.org/u/jasim)\
**Post date:** [May 30, 2017, 6:16pm UTC](https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300/1 "2017-05-30T18:16:50Z")

</div>

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}
  };
```

---

<div class="post-metadata">

**Author:** ![jasim](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jasim/32/168_2.png) [@jasim](https://discuss.ocaml.org/u/jasim)\
**Post date:** [May 30, 2017, 11:06pm UTC](https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300/2 "2017-05-30T23:06:50Z")

</div>

Asked a bit too early. Real World OCaml has covered this - move common fields into a parent type. [https://realworldocaml.org/v1/en/html/variants.html](https://realworldocaml.org/v1/en/html/variants.html).

This is my new code:

```
type rect = {height: int};
type details = 
  | Rect rect
  | Square;
type shape = {width: int, details};

let widerShape shape :shape => {...shape, width: shape.width + 100};
```

---

<div class="post-metadata">

**Author:** ![Yaron\_Minsky](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yaron_minsky/32/11_2.png) [@Yaron\_Minsky](https://discuss.ocaml.org/u/Yaron_Minsky)\
**Post date:** [May 30, 2017, 11:19pm UTC](https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300/3 "2017-05-30T23:19:57Z")

</div>

This is not yet documented in RWO, but you can also embed records in variants now, so you can write:

```ocaml
type details = 
  | Rect of { height : int }
  | Square
type shape = {width : int; details : details };

```

which is ever so slightly more concise, and also a bit more efficient.

I’m not sure what the Reason embedding of that is, though.

---

<div class="post-metadata">

**Author:** ![jasim](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jasim/32/168_2.png) [@jasim](https://discuss.ocaml.org/u/jasim)\
**Post date:** [May 30, 2017, 11:55pm UTC](https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300/4 "2017-05-30T23:55:45Z")

</div>

That’s nice, thanks. Reason doesn’t seem to support inline records in variants yet. But I’m sure it’ll catch up. 🙂

---

<div class="post-metadata">

**Author:** ![vramana](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vramana/32/12_2.png) [@vramana](https://discuss.ocaml.org/u/vramana)\
**Post date:** [May 31, 2017, 6:49am UTC](https://discuss.ocaml.org/t/how-to-access-identical-fields-from-variant-constructors-without-pattern-matching/300/5 "2017-05-31T06:49:46Z")

</div>

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
