Ppx_deriving eq access to function

Hi,

Say I have a type

t = A | B of t list

Where I want to implement an equal function with the order of t in B not matter. Since the real type has a lot of constructors, I would like to use ppx_deriving.eq. Overriding the default equality on a specific constructor is easy, fortunately:

t = | A | B of t list [@equal fun a b -> ...] [@@deriving eq]

The issue here is that to implement it correctly I would need to call the generated equal function recursively on each t in B. But how do I get a reference to the function that will be called equal : t -> t -> bool once ppx_deriving is done?

In other words, I am looking for the let rec name of the function my @equal will be part of.

1 Like

The name that ppx_deriving generates is predictable and is bound within the generated function, so you can simply use that:

type t =
  | A
  | B of t [@equal fun x -> equal x]
[@@deriving eq]

type zonk =
  | C
  | D of zonk [@equal fun z -> equal_zonk z]
[@@deriving eq]

If I remember correctly, you can’t use the [@equal ...] attribute to define a custom equality on a specific variant with ppx_deriving, only on a specific field of a record. That would be a very nice addition though!

This explains why it compiles and runs successfully even when I write completely bogous things there. Thanks for the explanation, looks like I will have to write that part manually.