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

Create a new multibinder alternate for Set<? extends T> #1366

Closed

Commits on Jul 22, 2020

  1. Create a new multibinder alternate for Set<? extends T>

    This is a simple linked binding to the original multibinder set. This is
    safe because 'Set<? extends T>' is assignable from 'Set<T>'.
    
    This is useful for projects integrating Guice and Kotlin. In Kotlin there
    are different types for read-only vs. read-write Sets. When declaring a
    read-only Set, the bytecode includes a wildcard.
    
    In particular, this Kotlin:
    
        var setA: Set<Runnable>
        var setB: MutableSet<Runnable>
    
    is equivalent to this Java:
    
        Set<? extends Runnable> setA;
        Set<Runnable> setB;
    
    Using Kotlin with Guice multibindings is quite annoying because the binding
    to `Set<Runnable>` is typically provided, but `Set<? extends Runnable>` is
    needed.
    
    The typical Kotlin workaround is also ugly:
    
        var setA: Set<@JvmSuppressWildcards Runnable>
    
    With this fix, Guice multibindings and Guice should just work.
    
    I expect this may be backwards-incompatible with a very small number of Kotlin
    applications that implement a different workaround to the above problem:
    
        @provides
        Set<? extends Runnable> provideRunnables(Set<Runnable> set) {
          return set;
        }
    
    I don't expect backwards incompatibility with Java programs. In such programs
    the behavior change will result in a fast failure (injector fails to create)
    which is easy to diagnose and fix.
    swankjesse committed Jul 22, 2020
    Configuration menu
    Copy the full SHA
    a5d2f01 View commit details
    Browse the repository at this point in the history