Lightweight interfaces instead of function types
Stephen Colebourne
scolebourne at joda.org
Sun Feb 21 03:58:13 PST 2010
>> Callable<String>[] array = new Callable[10];
>> This works, and is safe /in practice/ (rather than type-system safe).
On 21 February 2010 06:46, Alex Buckley <Alex.Buckley at sun.com> wrote:
> No, it isn't safe in practice. It causes heap pollution which can cause
> programs to crash. It is a hack allowed because migration compatibility
> was so very, very important. (It is not particularly helpful of IDE
> vendors to hide the unchecked warning by default.) Reified generics
> don't help either; they just make the topic 5x more complicated.
On 21 February 2010 07:37, Neal Gafter <neal at gafter.com> wrote:
> It is because people like you have been teaching this "safe in practice"
> attitude that people write code like that... code that would break hard if
> generics were ever reified. The existence of so much code that would break
> is a principal obstacle to actually reifying generics.
The "safe in practice" point is that for the vast majority of
developers, for the vast majority of time, this will never be an
issue. Arrays are relatively rarely assigned to another array, and
arrays are relatively rarely used in method signatures
Its also important to say that most developers don't understand the
detailed reason why it has to produce a warning, they just know that
lots of things in generics suck. I expect the sequence of events to be
something like:
Developer writes:
Callable<String>[] = new Callable<String>[1];
Gets compile error.
Curses sucky generics.
Thinks "how can I work around generics".
Thinks "this would work without generics".
Changes code:
Callable<String>[] = new Callable[1];
Gets warning.
Curses sucky generics.
Clicks IDE "get rid of warnings" button.
Carries on with what they were actually trying to do.
The developer is just interested in getting their task done, not
keeping the compiler happy. Taken from that perspective, its not
unreasonable to wonder if the error and warning actually accomplished
anything?
I'd also note that varargs use arrays, and with the Coin fix we will
see more generified arrays, not less.
More broadly, it has been noted by more than one person that
ClassCastExceptions from ungenerified objects/collections or
ArrayStoreExceptions have always been a rarity in real code by
comparison to NullPointerExceptions (ie. why aren't we tackling
NPEs???).
I'm not trying to start a debate on this - its partly philosophical.
But I do feel its very important to make the point that, IMO, most
developers don't worry about these things to the extent that language
designers do. Writing Callable<String>[] array = new Callable[10]; has
become just a necessary part of using Java today.
Stephen
More information about the lambda-dev
mailing list