-
Notifications
You must be signed in to change notification settings - Fork 43
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
Generate visitor interfaces for conjure enums #142
Generate visitor interfaces for conjure enums #142
Conversation
Most uses of enums involve a switch or if-else block across values along the lines of "boolean isTerminalState(enum)" or "String getDescription(enum)". It's easy to add new enum values without updating all usages, by using a visitor we catch these bugs at compile time rather than runtime.
Tests for generated enums
Mentioned on #134, but I think we should strongly consider dropping enums in favor of unions and/or switching enum code-gen to use the union code-gen, as enums are really a subset of the union functionality. |
conjure-java-core/src/main/java/com/palantir/conjure/java/types/EnumGenerator.java
Outdated
Show resolved
Hide resolved
@markelliot One possible migration path is to keep the conjure frontend the same but compile both enum and union to the same internal representation by adding a
would be equivalent to:
(can shorten to lit) and an instance of UnionFoo.a would generate the actual type in the ir: Examples of literal types in other languages: |
I think a special “none” type would be cleaner and avoid opening up enums to also be used as constants. It’d also satisfy a common union pain point. After collapsing the codegen, the union codegen probably needs some much closer inspection for clarity and perf. |
This proposal will cause issues with header and query parameter enum values, we would have to validate that all enum types are lit, and cannot ever receive non-lit values. fwiw I think the use cases for union and enums are fairly distinct, if anything I'd prefer to remove the specifier key from the union type which encroaches on the enums territory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Lots of the arguments for why we recommend visitors for unions also apply to enums, so I think adding visitor codegen here makes a bunch of sense. Whether we want to eventually unify them or not, I think this is a step in the right direction.
I think discussions about whether we should drop enums completely warrants a dedicated ticket on palantir/conjure as this would affect all languages.
Planning to merge tomorrow unless there are strong reservations. |
Most uses of enums involve a switch or if-else block across values
along the lines of "boolean isTerminalState(enum)" or
"String getDescription(enum)". It's easy to add new enum values
without updating all usages, by using a visitor we catch these
bugs at compile time rather than runtime.