# Correct syntax to add compare ppx

**URL:** <https://discuss.ocaml.org/t/correct-syntax-to-add-compare-ppx/15764>\
**Category:** Learning\
**Tags:** ppx\
**Created:** [December 6, 2024, 5:52pm UTC](https://discuss.ocaml.org/t/correct-syntax-to-add-compare-ppx/15764 "2024-12-06T17:52:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![satyajit\_sarangdhar](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/satyajit_sarangdhar/32/5666_2.png) [@satyajit\_sarangdhar](https://discuss.ocaml.org/u/satyajit_sarangdhar)\
**Post date:** [December 6, 2024, 5:52pm UTC](https://discuss.ocaml.org/t/correct-syntax-to-add-compare-ppx/15764/1 "2024-12-06T17:52:25Z")

</div>

type color =  
| Red  
| Yellow  
| Green

type stoplight =  
{ location : string  
; mutable color : color  
}[@@deriving compare]

Is this the correct ppx way to add compare property to type stoplight?

---

<div class="post-metadata">

**Author:** ![patricoferris](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/patricoferris/32/3621_2.png) [@patricoferris](https://discuss.ocaml.org/u/patricoferris)\
**Post date:** [December 7, 2024, 12:12pm UTC](https://discuss.ocaml.org/t/correct-syntax-to-add-compare-ppx/15764/2 "2024-12-07T12:12:08Z")

</div>

Hey @satyajit_sarangdhar,

I’m assuming you are using the [`ppx_compare`](https://ocaml.org/p/ppx_compare) deriver. You are going to want to make sure that every type that `stoplight` depends on also has a comparison function available. In this case that is `string` and `color`. The `string` comparison function is provided by the ppx itself, to bring it into scope you will need to add `open Ppx_compare_lib.Builtin` before the definition of `stoplight`. As for `color`, you will want to also derive a `color_compare` function using `[@@deriving compare]` on that type declaration. All together this looks like:

```ocaml
open Ppx_compare_lib.Builtin

type color =
| Red
| Yellow
| Green [@@deriving compare]

type stoplight = { 
  location : string; 
  mutable color : color
}[@@deriving compare]

```

with the following dune file:

```dune
(library
 (name lib)
 (preprocess (pps ppx_compare)))

```

Also, the discuss forum supports code blocks which can make reading code fragments easier both for you and readers. You can add a code block by surrounding your code in backticks (see this [guide on fenced code blocks](https://www.markdownguide.org/extended-syntax/#fenced-code-blocks)).

Hope this helps :))

---

<div class="post-metadata">

**Author:** ![satyajit\_sarangdhar](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/satyajit_sarangdhar/32/5666_2.png) [@satyajit\_sarangdhar](https://discuss.ocaml.org/u/satyajit_sarangdhar)\
**Post date:** [December 7, 2024, 4:39pm UTC](https://discuss.ocaml.org/t/correct-syntax-to-add-compare-ppx/15764/3 "2024-12-07T16:39:38Z")

</div>

Thanks a bunch! I didn’t realize you had to add the [@@] statement in two places but that does make sense. Will follow the guidelines for next time 😃
