Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Eclipse APIs use Arrays instead of Collections?

In the Eclipse APIs, the return and argument types are mostly arrays instead of collections. An example is the members method on IContainer, which returns IResources[].

I am interested in why this is the case. Maybe it is one of the following:

  1. The APIs were designed before generics generics were available, so IResource[] was better than just Collection or List
  2. Memory concerns, e.g. ArrayList internally holds an array which has more space than is needed (to offer an efficient implementation of add), whereas an array is always constructed for just the needed target size
  3. It's not possible to add/remove elements on an array, so it is safe for iterating (but defensive copying is still necessary, because one can still change elements, e.g. set them to null)

Does anyone have any insights or other ideas why the API was developed that way?

like image 380
robinst Avatar asked Nov 13 '22 01:11

robinst


1 Answers

Posting this as an answer, so it can be accepted.

Eclipse predates generics and they are really serious about API stability. Also, at the low level of SWT passing arrays seems to be used to reflect the operating system APIs that are being wrapped. Once you have a bunch of tooling using Arrays I guess it makes sense to keep things consistent. Also note that arrays aren't subject to all of the type erasure issues when using reflection.

Yeah, I hear you as far as the collections api being generally much easier to work with for dynamic lists of items.

like image 68
Robert Avatar answered Dec 21 '22 01:12

Robert