During a discussion with Konrad Malawski about typesystems.
Consider a Fruit.
class Fruit {}
There are different kinds of fruit. Explicitly:
Fruit<E> {}
So far so good.
Fruit<Apple> apple; Fruit<Orange> orange;
Which allows:
apple.compareTo(orange)
Does this make sense? Well, no 🙂 What to do? Well, Types to the rescue.
Dear types, please make comparing Fruits of different kinds illegal.
The types listen, and respond. Or actually it’s already in the JDK.
java.lang.Enum, which does
class Enum<E extends Enum<E>>
Generalizing
class Container<Type extends Container<Type>> Why? To save the types. Applied to the example: [code lang="java"]class Fruit<E extends <Fruit<E>> {}
now makes
orange.compareTo(apple)
illegal. As expected and while still in Java.