-
Notifications
You must be signed in to change notification settings - Fork 657
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
[compiler] Add Enum.KNOWN__ as an intermediary interface #6248
Conversation
❌ Docs Preview FailedError
|
public abstract interface annotation class com/apollographql/apollo/annotations/ApolloPrivateEnumConstructor : java/lang/annotation/Annotation { | ||
} | ||
|
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.
This introduces the Opt-in marker that we removed in d160e98.
The underlying reason is that Kotlin doesn't support static factory method (companion obejcts cannot access private constructors of nested classes).
The previous trick of extracting a public interface doesn't work with sealed classes because the compiler insists on having UNKNOWN__Impl
be part of the exhaustive when
statements and it's private in the file. Maybe a future version of the compiler could fix that but as of today I haven't found a way.
f591ce0
to
7bbc913
Compare
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.
🚀
...ies/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/Identifiers.kt
Show resolved
Hide resolved
...tlin/com/apollographql/apollo/compiler/codegen/kotlin/schema/EnumAsSealedInterfaceBuilder.kt
Outdated
Show resolved
Hide resolved
...apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/api/Service.kt
Outdated
Show resolved
Hide resolved
…ollographql/apollo/gradle/api/Service.kt Co-authored-by: Benoit 'BoD' Lubek <[email protected]>
…ollo/compiler/codegen/kotlin/schema/EnumAsSealedInterfaceBuilder.kt Co-authored-by: Benoit 'BoD' Lubek <[email protected]>
Closes #6243
Generated code
At a high level, this change introduces an intermediate
KNOWN__
interface, symmetrical withUNKNOWN__
:Usage
This allows unwrapping an unknown value to a known one to provide a "default":
This way, callers don't have to switch on the unknown case:
Compatibility
I contemplated adding another option but ultimately decided to reuse
sealedClassesAsEnumMatching
.This is a source-compatible change but a binary breaking change because of the changing of
UNKNOWN__
from interface to class.See https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-5.html#jvms-5.4.3.4:
You are impacted if you are shipping a library that exposes a generated
SomeEnum.UNKNOWN__
in its public API.I'm hoping that this pattern is not too widespread. If it is, and you're reading this, past me apologize in advance 😬 .