This method signature is "useful", in the sense that you can implement non-trivial, non-degenerate methods with it (that is, returning null
and throwing errors are not your only options). As the following example shows, such a method can be useful for implementing some algebraic structures like e.g. monoids.
First, observe that List<? extends T>
is a type with the following properties:
- You know that all elements of this list conform to the type
T
, so whenever you extract an element from this list, you can use it in position where a T
is expected. You can read from this list.
- The exact type is unknown, so you can never be certain that an instance of a particular subtype of
T
can be added to this list. That is, you effectively cannot add new elements to such a list (unless you use null
s / type casts / exploit unsoundness of Java's type system, that is).
In combination, it means that List<? extends T>
is kind-of like an append-protected list, with type-level append-protection.
You can actually do meaningful computations with such "append-protected" lists. Here are a few examples:
You can create append-protected lists with a single element:
public static <T> List<? extends T> pure(T t) {
List<T> result = new LinkedList<T>();
result.add(t);
return result;
}
You can create append-protected lists from ordinary lists:
public static <T> List<? extends T> toAppendProtected(List<T> original) {
List<T> result = new LinkedList<T>();
result.addAll(original);
return result;
}
You can combine append-protected lists:
public static <T> List<? extends T> combineAppendProtected(
List<? extends T> a,
List<? extends T> b
) {
List<T> result = new LinkedList<T>();
result.addAll(a);
result.addAll(b);
return result;
}
And, most importantly for this question, you can implement a method that returns an empty append-protected list of given type:
public static <T> List<? extends T> emptyAppendProtected() {
return new LinkedList<T>();
}
Together, combine
and empty
form an actual algebraic structure (a monoid), and methods like pure
ensure that it's non-degenerate (i.e. it has more elements that just an empty list). Indeed, if you had an interface similar to the usual Monoid typeclass:
public static interface Monoid<X> {
X empty();
X combine(X a, X b);
}
then you could use the above methods to implement it as follows:
public static <T> Monoid<List<? extends T>> appendProtectedListsMonoid() {
return new Monoid<List<? extends T>>() {
public List<? extends T> empty() {
return ReadOnlyLists.<T>emptyAppendProtected();
}
public List<? extends T> combine(
List<? extends T> a,
List<? extends T> b
) {
return combineAppendProtected(a, b);
}
};
}
This shows that methods with the signature given in your question can be used to implement some common design patterns / algebraic structures (monoids). Admittedly, the example is somewhat contrived, you probably wouldn't want to use it in practice, because you don't want to astonish the users of your API too much.
Full compilable example:
import java.util.*;
class AppendProtectedLists {
public static <T> List<? extends T> emptyAppendProtected() {
return new LinkedList<T>();
}
public static <T> List<? extends T> combineAppendProtected(
List<? extends T> a,
List<? extends T> b
) {
List<T> result = new LinkedList<T>();
result.addAll(a);
result.addAll(b);
return result;
}
public static <T> List<? extends T> toAppendProtected(List<T> original) {
List<T> result = new LinkedList<T>();
result.addAll(original);
return result;
}
public static <T> List<? extends T> pure(T t) {
List<T> result = new LinkedList<T>();
result.add(t);
return result;
}
public static interface Monoid<X> {
X empty();
X combine(X a, X b);
}
public static <T> Monoid<List<? extends T>> appendProtectedListsMonoid() {
return new Monoid<List<? extends T>>() {
public List<? extends T> empty() {
return AppendProtectedLists.<T>emptyAppendProtected();
}
public List<? extends T> combine(
List<? extends T> a,
List<? extends T> b
) {
return combineAppendProtected(a, b);
}
};
}
public static void main(String[] args) {
Monoid<List<? extends String>> monoid = appendProtectedListsMonoid();
List<? extends String> e = monoid.empty();
// e.add("hi"); // refuses to compile, which is good: write protection!
List<? extends String> a = pure("a");
List<? extends String> b = pure("b");
List<? extends String> c = monoid.combine(e, monoid.combine(a, b));
System.out.println(c); // output: [a, b]
}
}