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

PPX: Support open variants #40

Open
jchavarri opened this issue Nov 28, 2024 · 4 comments
Open

PPX: Support open variants #40

jchavarri opened this issue Nov 28, 2024 · 4 comments

Comments

@jchavarri
Copy link
Member

jchavarri commented Nov 28, 2024

Sometimes there are type definitions like this:

type status = [> `Foo | `Bar ]

let handle_status (status : status) : string =
  match status with
  | `Foo -> "It's Foo!"
  | `Bar -> "It's Bar!"
  | _ -> "Unknown status."

This is useful because this code is "future compatible", meaning a server could add new cases without the client crashing, or the other way around.

Would be nice to support it in melange-json PPX.

@jchavarri jchavarri changed the title Support open variants PPX: Support open variants Nov 28, 2024
@anmonteiro
Copy link
Member

anmonteiro commented Nov 28, 2024

in [> `Foo | `Bar ] there's a type variable like there's a type variable in 'a StringMap.t. it would be even nicer to provide the function for 'a as well to complete the printing :)

@andreypopp
Copy link
Collaborator

I was referring to this .atd's open enum feature https://atd.readthedocs.io/en/latest/atdgen.html#field-open-enum which works either for poly or regular variants.

@andreypopp
Copy link
Collaborator

Position: on a variant type comprising exactly one constructor with an argument. The type of that argument must be string. All other constructors must have no arguments.

Hm... wondering what's the motivation for this restriction.

@anmonteiro
Copy link
Member

Hm... wondering what's the motivation for this restriction.

probably a small optimization? if you don't allocate + create all ints + one string for the one with payload, you save a little bit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants