ELI5 how type erasure works

Imagine you have a box where you can put toys, but you want to make sure that you only put action figures in one box and cars in another, so you don't mix them up. In the world of programming, we have something similar called "types" that help us organize our data, like toys, so we know what is what.

Now, let's say you have a magic box that can change what it holds based on a magic word you say. You can say "action figures," and it will become a box for action figures. Or you can say "cars," and it changes to hold cars. In programming, this magic box is called "generics," because it can change based on what we need.

But, there's a problem. Our magic boxes are made by a very old magic that doesn't remember the magic word (type) you said after you've said it. So, if you ask the box later, "Are you holding action figures?" it can't tell you because it forgot. This forgetting is called "type erasure." It's like if you put a label on the box saying what's inside, but the label disappears after you put it on.

Why do we need this forgetting, though? Well, long ago, when the magic was created (when programming languages like Java were made), the wizards decided that all the old magic boxes (older programs) needed to work with the new magic without having to be remade. To make sure that everyone could still use their old boxes the same way they always did, even with the new magic words, they made the magic forget the words after using them. This way, old and new magic boxes could work together, and everyone could be happy.

In simple terms, we need type erasure to make sure our new and fancy way of organizing things (with generics) can still work with all the old ways without causing any problems. It's a bit like making sure that all your toys, old and new, can still fit in the toy chest at home, even if you can't see the labels on them anymore.