Skip to content

PPX: Support open variants #40

Open

Description

@jchavarri

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.

Activity

  1. changed the title [-]Support open variants[/-] [+]PPX: Support open variants[/+] on Nov 28, 2024
  2. anmonteiro commented on Nov 28, 2024

    @anmonteiro
    Member

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

  3. andreypopp commented on Nov 28, 2024

    @andreypopp
    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.

  4. andreypopp commented on Nov 28, 2024

    @andreypopp
    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.

  5. anmonteiro commented on Nov 28, 2024

    @anmonteiro
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions