-
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
[improvement] Generating Visitors for conjure enums #132
Conversation
Thanks for your interest in palantir/conjure-java, @jerzozwierz! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
We're generally happy with the switch pattern (because it's more concise than the visitor pattern) in conjunction with static analysis to ensure all cases are covered, for instance via error-prone, see https://github.com/google/error-prone/blob/master/docs/bugpattern/MissingCasesInEnumSwitch.md . |
Would that same pattern work for you, @jerzozwierz ? |
I'm not exactly sure, @uschi2000. Consider the following example:
Since the default clause is present, error-prone will not complain (at least according to my understanding, happy to be proven wrong). |
Your analysis is correct. Interestingly, I can't actually remember encountering such a case in practice. Thoughts, @markelliot ? |
I see your point, and I obviously agree that using plain-vanilla switch is much simpler and concise than creating visitors here and there. Nevertheless, I'm still pretty convinced that we should leave the decision whether to rely on static analyzers or to have compile-time guarantees out of the box (i.e. use a Visitor), at a cost of slightly less pretty code. |
Also tagging @iamdanfox for thoughts |
I didn't see this PR when I put together #142, apologies for duplicating effort. Strongly in favor visitors for enums, it's much easier to build code to fail when cases aren't all covered. Edit: |
Error-prone is a great tool, but I don't think we should lean it for problems that the java compiler is a better fit for. Furthermore I'm not confident that all applications using conjure have this error-prone check enabled, and more importantly we cannot force all consumers of conjure-generated clients to enable the right checks. |
I have also encountered the problem mentioned by @jerzozwierz. This is especially problematic if an API adds new enum values, since this has the potential to silently cause breaks. In palantir/gradle-baseline#481, I added a error-prone check that suggests avoiding default cases in switch statements to ensure that the MissingCasesInEnumSwitch check catches these types of bugs. |
Thanks for pushing this issue forwards @jerzozwierz, just closing out to focus on #142 as I think it achieves the same outcomes. |
Before this PR
Although Conjure Unions support the visitor pattern out of the box, Enums did not. It is error-prone for server developers, as it's too easy to add an extra value to an enum and forget to update one of the
switch
clauses referring to it.After this PR
Server and client maintainers have flexibility in choosing whether they want enum updates to cause a compile break.