โ Back to Generics and type erasure
๐ท๏ธ Generics and type erasure: cheat sheet
๐จ The analogyBoxes labelled "apples only" are checked at the hall door, but the labels come off at the factory gate.
๐ The regular explanationHow Java generics let the compiler check element types and drop casts, why List<Integer> is not a List<Number>, how wildcards follow PECS, and what type erasure removes at run time.
- In the old days the boxes were plain. Ana packs an apple and then, by mistake, a pear. Nobody stops her.
- Ben takes the second item out, expecting an apple. It's a pear. The mistake shows up here, far from where Ana made it.
- Now the box has a label, "apples only", and a checker stands at the door. Ana tries the pear again and the checker turns it back at once.
- The apple passes the check and goes in. Ben takes it out without inspecting it, because the label promises it's an apple.
- The factory has one box design with a blank on the label. Fill in "apples" and you get an apples box.
- The same design, with "bricks" in the blank, makes a bricks box. No new design needed.
- Cleo's job card says "pick the ripest from any fruit box". Handed the apples box, she hands back an apple.
- Someone hands Cleo the bricks box. Her card says fruit only, so the checker stops it before she ever starts.
- Ben wants to call the apples box "a fruit box". The checker refuses, because then he could drop a pear in.
- The old wooden crate has its label burnt in, so the checker lets Ben call it a fruit crate.
- Ben puts an apple in. Fine.
- Then a pear. The crate reads its burnt-in label and refuses, right in the middle of the work instead of at the door.
- Cleo takes from a box of "some kind of fruit". Today it's apples. Whatever she pulls out is surely a fruit.
- She tries to put a pear back into that box. Not allowed. It might be the apples box, and a pear would be wrong.
- She puts the fruit into a box that accepts "fruit or anything bigger". Always safe.
- If she takes something back out of that box, all she knows is that it's "a thing". So take from "some kind of", put into "or bigger".
- In the packing hall the box says "apples only", and the checker makes sure every item fits the label.
- At the factory gate the label is peeled off. Downstairs it's a plain crate, with a little "check it's an apple" note where things come out.
- In the yard someone asks "is this an apples crate?" and "make me one more of whatever goes in here". No label left, so nobody can answer.
- So Ana sends a label card along on purpose. The yard reads "apple" off the card and can make a new apple.
- The apples box holds an apple. Bob treats it as a plain box, so the checker can't tell what he puts in. It can only warn him.
- Bob drops a pear into the apples box. Nothing stops it and nothing notices. The box is now polluted.
- Much later Ben takes out "an apple" and gets the pear. Things go wrong for Ben, far from Bob.
- Cleo's card takes a bundle in a plain sack, so the checker warns too. She only reads from it, so she signs a promise and the warning goes away.
๐งฉ Key questions
- Ana tries to drop a pear into a box labelled "apples only". When is the mistake caught?
- Right away, by the checker at the door. That's the point of the label: the checker stops the pear before it goes anywhere.
- Why won't the checker let Ben call an apples box "a fruit box"?
- Because he could then put a pear into the apples box. An apples box is an apples box. Wooden crates allow the name but refuse the pear later, mid-work.
- Cleo moves fruit from one box into another. Which labels should her job card ask for?
- Take from "some kind of fruit", put into "fruit or anything bigger". What comes out of a "some kind of fruit" box is surely fruit, and a "fruit or bigger" box safely takes fruit.
- Downstairs, someone asks a plain crate to make one more of whatever goes inside it. What does Ana need to do?
- Send a label card along that says "apple". Labels are peeled at the factory gate, so the type has to travel on purpose.
- Bob treats the apples box as a plain box and drops a pear in. When does something go wrong?
- Later, when someone takes the pear out expecting an apple. The checker could only warn Bob, and the plain crate can't tell, so the problem shows up far away.