Had a curious chat with a colleague after returning to work from ScalaSwarm conference.
- Me: I brought a cool T-shirt from Porto.
- Him: Is it a Scala T-shirt?
- Me: No, but it’s still cool. T-shirt is an invariant type with regards to coolness.
That seemed like a good joke at that moment, but later I realized that variance is applicable only to subtyping relationships, and not to coolness. What I actually meant was that if there are cool and normal things:
sealed trait Cool
object Scala extends Cool // cool
object CommunistParty // not cool
Then this is perfectly possible:
class `Cool_T-Shirt`[T] extends Cool
val tShirt = new `Cool_T-Shirt`[CommunistParty.type]
It would not be possible of there was an upper type bound on type parameter of Cool_T-Shirt
type, that would allow only Cool things to be passed into.
class `Cool_T-Shirt`[T <: Cool]
val tShirt = new `Cool_T-Shirt`[CommunistParty.type]
// Error:(9, -69) type arguments [A$A64.this.CommunistParty.type] do not conform to class Cool_T-Shirt's type parameter bounds [T <: A$A64.this.Cool]
// val tShirt = new `Cool_T-Shirt`[CommunistParty.type];}
// ^
In normal words, that would mean: T-Shirts can be cool only if the thing depicted on it is cool, which is not true, in my opinion. To make a good joke about this I should have said:
- No, but it’s still cool.
Cool_T-shirt
doesn’t have bounds on type of the image.