How can a interface Stream extend Base stream interface having type bounds just as the stream interface i.e. Stream <T>?
Asked Answered
O

1

5

I was going through Streams Documentation and had observed that Stream is a typed interface that extends Base Stream with types as T and again Stream <T>

public interface Stream<T> extends BaseStream<T,Stream<T>> {
}

How can a interface Stream can extend Base stream interface having type bounds just as the stream interface i.e. Stream <T>?

Oogonium answered 16/4, 2022 at 18:37 Comment(1)
Actually, I think this is a very good Question. Defining something in terms of itself does pose a conundrum.Spermatophyte
C
7

Why couldn't it?

This is a trick used in many places, for example enums, which are defined as:

public abstract class Enum<E extends Enum<E>>

The general idea is to establish a type parameter that represents your own type.

That's why that parameter's letter is S. That stands for "Self".

Hence, if we look at DoubleStream, it is defined as:

public interface DoubleStream extends BaseStream<Double, DoubleStream> {}

The point of doing this is for 'self returning' methods, or at least, for methods that need to return the same type. There are loads of those in the stream concept, such as peek, or limit, or onClose.

You could just.. not do that, and define things as follows:

public interface BaseStream<T> {
  BaseStream parallel();
}

But the problem is, this downgrades types. If I have an IntStream and I invoke .parallel() on it, the compiler thinks the type of that is BaseStream. This isn't usually relevant (in that IntStream can tighten the return type, that's valid as per the Java Lang Spec), but sometimes it is.

Cottingham answered 16/4, 2022 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.