Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GADT support #8

Closed
ejgallego opened this issue Jun 8, 2016 · 3 comments
Closed

GADT support #8

ejgallego opened this issue Jun 8, 2016 · 3 comments
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@ejgallego
Copy link

Dear ppx_sexp_conv devs,

thanks again for this great tool, it would be great if GADTs could be serialized both ways. This:

type _ obj =
  | OInt :    int obj
  | OStr : string obj
[@@deriving of_sexp]

currently results in:

Error: This expression has type int obj
       but an expression was expected of type v_x__001_ obj
       Type int is not compatible with type v_x__001_ 

Note that [@@deriving sexp_of] works fine and it indeed is very useful!

@ghost
Copy link

ghost commented Jun 8, 2016

Unfortunately it's not possible to write such a function as it would be unsafe. Instead you need to wrap the type into a GADT to introduce the existential:

type packed_obj = Obj : _ obj -> packed_obj

Currently there is no special support in findlib for types that are wrapped in such a way. What we usually do is define a helper type:

module For_sexp = struct
  type t = OInt | OStr [@@deriving of_sexp]
end

type _ t = OInt : int t | OStr : string t

type packed = T : _ t -> packed

let packed_of_sexp sexp =
  match For_sexp.t_of_sexp with
  | OInt -> T OInt
  | OStr -> T OStr

@ejgallego
Copy link
Author

ejgallego commented Jun 8, 2016

Oh indeed, I see!

Thank you very much for the reply @diml .

@aalekseyev
Copy link
Contributor

I'm closing this issue because the specific question got answered. The general support of GADTs is a complicated topic and definitely not fully explored. Any proposals for how to support GADTs are welcome in separate issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

3 participants