# Help: simplify this code

**URL:** <https://discuss.ocaml.org/t/help-simplify-this-code/11775>\
**Category:** Learning\
**Created:** [March 24, 2023, 5:53pm UTC](https://discuss.ocaml.org/t/help-simplify-this-code/11775 "2023-03-24T17:53:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 24, 2023, 5:53pm UTC](https://discuss.ocaml.org/t/help-simplify-this-code/11775/1 "2023-03-24T17:53:44Z")

</div>

```auto
  type source_loc = { row : int; col : int } [@@deriving compare, equal]

  let conv_loc (pos : Sexplib.Src_pos.Relative.t) : source_loc =
    match pos with
    | { row; col } -> { row; col }

```

Why I am defining this in the first place: for the `[@@deriving compare, equal]`

the conv\_loc code seems convoluted; is there a way to simplify this function ?

Thanks!

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [March 24, 2023, 6:13pm UTC](https://discuss.ocaml.org/t/help-simplify-this-code/11775/2 "2023-03-24T18:13:32Z")

</div>

You can re-export types to let ppx derivers works on the full type definition while keeping the two types equal:

```ocaml
type source_loc = Sexplib.Src_pos.Relative.t = { row : int; col : int }
  [@@deriving compare, equal]

```

---

<div class="post-metadata">

**Author:** ![lukstafi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lukstafi/32/3442_2.png) [@lukstafi](https://discuss.ocaml.org/u/lukstafi)\
**Post date:** [March 24, 2023, 8:21pm UTC](https://discuss.ocaml.org/t/help-simplify-this-code/11775/3 "2023-03-24T20:21:15Z")

</div>

> [@zeroexcuses](#):
>
> ```ocaml
> let conv_loc (pos : Sexplib.Src_pos.Relative.t) : source_loc =
> match pos with
> | { row; col } -> { row; col }
> 
> ```

Other things being equal, this should be the same as:

```ocaml
let conv_loc ({ row; col } : Sexplib.Src_pos.Relative.t) : source_loc = { row; col }

```

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [March 24, 2023, 8:28pm UTC](https://discuss.ocaml.org/t/help-simplify-this-code/11775/4 "2023-03-24T20:28:18Z")

</div>

> [@octachron](#):
>
> You can re-export types to let ppx derivers works on the full type definition while keeping the two types equal

This looks like a gap in my OCaml knowledge. Where did you learn this ?

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [March 24, 2023, 8:43pm UTC](https://discuss.ocaml.org/t/help-simplify-this-code/11775/5 "2023-03-24T20:43:38Z")

</div>

Since this is not that intuitive for people not (very?) familiar with ppxs and [types definitions](https://ocaml.org/manual/typedecl.html#ss:typedefs), this is explained in `ppx_deriving` documentation: [GitHub - ocaml-ppx/ppx\_deriving: Type-driven code generation for OCaml](https://github.com/ocaml-ppx/ppx_deriving#working-with-existing-types) .
