Hacker News new | comments | show | ask | jobs | submit login

Java has a nice solution: An ArrayList<Ellipse> is an ArrayList<? extends Shape>. But it is also an ArrayList<? super Circle>.

The former is "read-only" (can read elements which are all guaranteed to be shapes). The latter is "write-only" (we can add circles to it, but not all elements in it are guaranteed to be circles).

I use quotes because it is not strictly true, but any writes or reads will be of the sort that they don't cause type problems. For instance you can call clear() on a ArrayList<? extends Shape>

Applications are open for YC Summer 2018

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