Covariance and contravariance 00:00:00 Make a prediction You will choose the safe direction for a producer and a consumer, then explain why a mutable collection generally cannot use both substitutions. Suppose a dog-only pen is passed to someone as a writable pen for any animal. They add a cat. Can its original owner still safely take out a dog? No. The next animal might be the cat. Reading dogs as animals is safe; granting permission to write any animal into dog storage is the step that breaks the guarantee. 00:00:31 Who may stand in for whom? Put a window in the dog pen but leave the gate locked. A visitor who only wants to see an animal can safely look. At a washing station, someone willing to wash every animal can also wash your dog. The window is a producer: values come out. The washing service is a consumer: values go in. A writable pen has both directions, so the simple one-way substitution cannot cover it. Variance describes how the subtype relation of a generic type follows the subtype relation of its argument. Dog is a subtype of Animal. What about Source of Dog and Source of Animal? If the rule is too loose, a cat ends up in a list of dogs, and the failure comes later and elsewhere. Java arrays are loose in this way, and every store into an array of objects may need a check because of it. 00:01:21 Choose the safe direction A kennel only produces dogs. Code that asks for any animal can safely read its next result, because every dog is an animal. A handler able to accept every animal can also serve a caller that only hands it dogs. Input positions therefore vary in the opposite direction from output positions. A mutable box that both reads and writes cannot generally use either substitution: somebody might write a cat into a box of dogs. Read a dog from a source wherever the caller only needs an animal. That is safe in this direction. Try letting the same caller insert a cat into a writable box of dogs. A later consumer expecting a dog would receive the wrong type, which explains the write restriction. 00:02:08 Choose the right Java bound A function copies dogs into a destination. A source that produces dogs or a subtype can use an upper-bounded wildcard, and a destination able to consume dogs can use a lower-bounded wildcard. Trace one actual write to see why a list of cats cannot be used as the destination. Copy a dog from the producer into the destination that accepts all animals. Reverse those endpoints and the source might supply a cat to a destination accepting only dogs. The type bound encodes exactly which side sends a value and which side receives it. Adding a cat through an animal-typed alias destroys the dog-only invariant even when every individual assignment looks harmless in isolation. Split read-only and write-only interfaces when clients need only one direction; in Java, use suitable upper and lower bounds for producer and consumer roles. In Java, a comparator can accept a function that reads a value and returns a sorting key. The input and the result travel in opposite directions. Consider a second example: adding several dogs to a collection of animals is safe because that destination can accept dogs. A destination that only accepts a particular subtype of dog cannot accept every animal. 00:03:25 Try an interview follow-up Declaration site or use site variance? At the declaration, the author writes it once and users see plain types, but the type must have only one direction. At the use, any type can be used in one direction, with more noise. I split reader and writer interfaces where I own the types. A source that only yields cats can supply an animal reader. An animal handler can receive a cat wherever a cat handler is required. Now try allowing writes to a collection of cats through an animal collection reference. Someone could insert a dog. Trace that write to explain why a mutable container needs stricter rules. 00:04:08 What to take with you Ask what can be done with T. If T only comes out, a source of dogs is a source of animals. If T only goes in, a washer of animals is a washer of dogs. If both, the type must match exactly. Functions follow from this: parameters go in, results come out. So a function may replace another if it accepts at least as much and returns at least as precise a result. PECS is the same sentence for Java collections.