Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One day the rote example finally made sense to me, and I go back to it every time I hear about variance.

Got a Container<Animal> and want to treat it as a Container<Cat>? Then you can only write to it: it's ok to put a Cat in a Container<Cat> that's really a Container<Animal>.

Reading from it is wrong. If you treat a Container<Animal> like a Container<Cat> and read from it, you might get a Dog instead.

The same works in reverse, treating a Container<Cat> like a Container<Animal> is ok for read, but not for write.



The usual way of phrasing this is, famously, https://en.wikipedia.org/wiki/A_white_horse_is_not_a_horse




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: