jextract/jni: Fix enums with unconvertible cases#755
Conversation
| """ | ||
| // This is unavailable because it contains unsupported cases. | ||
| private Case getCase() { | ||
| return null; |
There was a problem hiding this comment.
We should probably throw an exception instead or just a stray null like that
| let allCasesCanBeTranslated = decl.cases.allSatisfy({ | ||
| translatedEnumCase(for: $0) != nil | ||
| }) | ||
| if !allCasesCanBeTranslated { |
There was a problem hiding this comment.
I'm not sure I understand -- wouldn't we want to just fail if we're in the not imported case in thew switch below?
There was a problem hiding this comment.
Either way works for me.
I implemented it this way because I thought it would be more user-friendly to make the function unavailable from the start, rather than causing a runtime error.
This is because users can still use alternatives like getAsNone instead of getCase to work around it.
I'm happy to change it to whichever you prefer.
Which would you prefer: throwing an exception for unsupported cases in the switch, or omitting the function entirely?
There was a problem hiding this comment.
Hm, if I read this correctly we'd make the entire getCase unavailable if any of the cases is not supported; but perhaps someone wants to use an enum with only a bunch of cases that were supported. So I think the dynamic may be ok... though we should emit warnings about the missed cases and maybe even some comments in source?
There was a problem hiding this comment.
So yeah, the throw variant: and we'd throw with some message that the case was not extracted because e.g. ... "type ... was not extracted"?
Currently, defining a generic enum like the one below leads to a Java compilation error:
This occurs because
some(T)contains a unsupported generic parameter and it is skipped.The Java switch expression becomes non-exhaustive, leading to the build failure.
In cases where a
getCasemethod cannot be fully generated due to unsupported generic parameters.Instead of producing uncompilable code, the generator now provides a dummy implementation instead.