The decision
A depot relay pulls exactly count parcels, forwarding each one in order. The express lane produces a subtype of Parcel. The archive accepts any object. Neither side needs to change its generic type to join this pipeline.
Read the Kotlin
Follow the direction a value travels
out T marks a producer: a reader expecting a Parcel can accept a source of Express parcels because each returned value is a Parcel. in T marks a consumer: a sink that accepts any Parcel is safe for a caller sending Express parcels. Pick one concrete item and check that it can make the trip without a cast.
val outgoing: Source<Parcel> = expressSource
val expressArchive: Sink<Express> = parcelArchiveRecheck variance when an interface grows
Read every use of T in the public methods. Adding a T input to a source would let someone pass an ordinary Parcel to a source backed by Express-only storage. Returning T from a broadly typed sink would let someone expect an Express from storage that accepts all parcels. A nullable return type would still carry the same unearned promise.
Trace one read and one write per repetition
repeat(count) runs its block once per repetition. In sink.put(source.next()) the source read produces the argument before the sink receives it. For two repetitions the order is read, write, read, write. At zero the block never runs. Pulling a value before entering the loop would charge for a read even with an empty batch.
The full example
import java.util.ArrayList;
import java.util.function.Supplier;
class Lesson {
static class Parcel {
final String code;
Parcel(String code) { this.code = code; }
}
static final class Express extends Parcel {
Express(String code) { super(code); }
}
interface Source<T> { T next(); }
interface Sink<T> { void put(T item); }
static void relay(Source<? extends Parcel> source, Sink<? super Parcel> sink, int count) {
for (int i = 0; i < count; i++) sink.put(source.next());
}
}open class Parcel(val code: String)
class Express(code: String) : Parcel(code)
fun interface Source<out T> { fun next(): T }
fun interface Sink<in T> { fun put(item: T) }
fun relay(source: Source<Parcel>, sink: Sink<Parcel>, count: Int) {
repeat(count) { sink.put(source.next()) }
}Inputs and outcomes
| Input | Outcome | Why it matters |
|---|---|---|
Express source P1, P2; count 2 | Sink receives P1, P2 | A producer of Express is safe wherever only Parcel is read. |
Parcel source; count 0 | No reads or writes | Checking an item in advance would consume a parcel. |
Object sink; one regular parcel | Sink receives that parcel | Anything able to accept every object can accept a Parcel. |
Walk through the code
- Source produces T, so Kotlin’s out declaration lets Source<Express> stand in for Source<Parcel>. The relay can always treat its next value as a Parcel.
- Sink consumes T. Its in declaration permits Sink<Any> where a Sink<Parcel> is required. Every parcel the relay can send is acceptable to that broader consumer.
- Java expresses the same directions at this use site with ? extends Parcel and ? super Parcel. Declaration-site variance in Kotlin makes the relationship available consistently at every use. Neither version needs a downcast in relay.
The Java habit
Subtyping of Express and Parcel does not automatically carry over to every generic class. An invariant mutable box cannot promise both safe reads and safe writes after the same substitution.
When to choose differently
When a queue both accepts and returns the same element type, keep the queue invariant. Expose separate restricted Source and Sink views to clients that only need one direction.