Correct syntax to add compare ppx

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?

Hey @satyajit_sarangdhar,

I’m assuming you are using the 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:

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:

(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).

Hope this helps :))

2 Likes

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 :smiley: