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

Record aliases #84

Open
alex35mil opened this issue May 12, 2020 · 0 comments
Open

Record aliases #84

alex35mil opened this issue May 12, 2020 · 0 comments

Comments

@alex35mil
Copy link
Member

The requirement of having record definitions inlined might be inconvenient when some type already exists and used across the app.

It's not possible to avoid inlining since PPX would need type information to generate the proper code, but it can be handled somewhat better:

// input alias
module Form = [%form
  [@alias "Article.t"]
  type input = {
    title: string,
    body: string,
  }
];

// collection alias
module Form = [%form
  type input = {
    title: string,
    authors: [@field.collection] array(author),
  }
  [@alias "Author.t"]
  and author = {name: string}
];

An inlined record can be aliased to an existing type in generated code. It would still be required to duplicate record definition so PPX can parse the internal types but the generated code and the rest of the app would be dealing with the provided type instead of the one defined in the PPX'ed module.

So the generated code would be:

// input alias
module Form = {
  type input = Article.t;
};

// collection alias
module Form = {
  type input = {
    title: string,
    authors: array(author),
  }
  and author = Author.t;
};

It should be totally type-safe, but errors might be confusing when an inlined definition and actual type don't match (no way it can be ensured by PPX, unfortunatelly).

Any other drawbacks?

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

No branches or pull requests

1 participant