janestreet / ppx_sexp_conv

Generation of S-expression conversion functions from type definitions
MIT License
88 stars 17 forks source link

type aliasing deriving is incomplete #3

Closed ivg closed 6 years ago

ivg commented 8 years ago

For a type definition three functions are derived:

   val __t_of_sexp__ : Sexplib.Sexp.t -> t
    val t_of_sexp : Sexplib.Sexp.t -> t
    val sexp_of_t : t -> Sexplib.Sexp.t

For a type alias only the latter two is derived. But it looks like that all three are needed to derive converters for a polymorphic variant type. Consider the following example:

# module Kind = struct 
      type t = [`Return | `Call] [@@deriving sexp]
    end;;
    module Kind :
  sig
    type t = [ `Call | `Return ]
    val __t_of_sexp__ : Sexplib.Sexp.t -> t
    val t_of_sexp : Sexplib.Sexp.t -> t
    val sexp_of_t : t -> Sexplib.Sexp.t
  end
# type kind = Kind.t [@@deriving sexp];;
type kind = Kind.t
val kind_of_sexp : Sexplib.Sexp.t -> kind = <fun>
val sexp_of_kind : kind -> Sexplib.Sexp.t = <fun>
# type pred = [`valid | kind] [@@deriving sexp];;
Characters 22-26:
  type pred = [`valid | kind] [@@deriving sexp];;
                        ^^^^
Error: Unbound value __kind_of_sexp__
ghost commented 8 years ago

The __t_of_sexp__ function is only needed for polymorphic variant. ppx_sexp_conv generates it only when the type is syntactically a polymorphic variant, for aliases you need to use [@@deriving sexp_poly]