home.social

#contravariant — Public Fediverse posts

Live and recent posts from across the Fediverse tagged #contravariant, aggregated by home.social.

fetched live
  1. CW: programming, java

    is there a name for the #ExistentialType pattern in #java to avoid wildcards (especially to reduce confusion about where the quantifiers are when returning a #HeterogenousCollection like Set<Foo<?>>)? I’m thinking along the lines of

    public sealed interface AnyFoo permits Foo {
        // Methods of Foo<T> that do not involve any T type variable
    }
    
    public non-sealed class Foo<T> implements AnyFoo {
        // Methods of Foo<T> that involve some T
    }

    I had students get very confused when the T type variable degrades to Object / Void based on the #covariant / #contravariant position, so enforcing the presence of an explicit cast might be beneficial. What is extra nice is that

    AnyFoo anyFoo;
    Foo<?> foo = (Foo<?>) anyFoo;

    is permitted without an unsafe cast warning due to the sealed keyword (although, to be honest, something like

    <T> void processFoo(Foo<T> foo);
    processFoo((Foo<?>) anyFoo);

    is much preferable – if you have a Skolem type variables lying around, the least you can do is naming them! :blobfoxscience:)