RandomAccess Interface and List Heirarchy
Cyrus Vafadari
cvafadari at gmail.com
Wed Sep 25 03:19:49 UTC 2019
Hello all,
*TLDR: Why doesn't RandomAccess interface extend List?*
I'm maintaining a framework that lets developers build plugins, and
developers implement a `put(List<Thing> thingList)` in their plugins.
However, I want to guarantee to the implementer that their List will
support RandomAccess. I see Java does support a syntax for declaring that
the argument should implement both List and RandomAccess using the "&"
operator, and I could declare my own interface that extends both, but I am
surprised that RandomAccess itself does not extend List and appear in the
List hierarchy. According to the docs, it only applies to List
implementations anyway, and we use `instanceof` extensively in the code to
bifurcate how we handle both cases. It seems more natural to me for
RandomAccess to extend List, so that I can let my implementer know
explicitly they can rely on RandomAccess.
I am happy to give more details on my scenario, and very excited to learn
more why this decision was made, and if there is an aspect I am missing or
if improvements could be made!
Best wishes,
Cyrus
More information about the core-libs-dev
mailing list