#contravariant — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #contravariant, aggregated by home.social.
-
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 ofpublic 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
Ttype variable degrades toObject/Voidbased on the #covariant / #contravariant position, so enforcing the presence of an explicit cast might be beneficial. What is extra nice is thatAnyFoo anyFoo; Foo<?> foo = (Foo<?>) anyFoo;is permitted without an unsafe cast warning due to the
sealedkeyword (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:)