Nicolai Parlog's blog post on intersection types with var shows a method, createCloseableIterator(), that returns an intersection type:
@SuppressWarnings("unchecked")
private static <T extends Closeable & Iterator<String>>
T createCloseableIterator(boolean empty) {
if (empty)
return (T) new Empty();
else
return (T) new Scanner(System.in);
}
where Empty and Scanner both implement Iterator<String> and Closeable.
Why are the explicit casts to (T) needed?