[Brian]: I find this nonintuitive, but the return type is evidently part of the method erasure even though it is not part of the method signature.
I agree. Even if it works, the original methods didn't have a need for any return value, so including one now seems very strange. I would suggest Michael might consider two other options:
1. Change the method names themselves (so we're no longer overloading at all):
2. Write a single, more general method:
(as long as we're doing something more general - if you don't do anything with the Collection other than iterate through it, then Iterable is all you need.)
[Brian]: (Frankly, I'm not sure what happens if the compiler has access only to the compiled .class file. Hmm.)
Should work the same. The .class file contains all the generic type info that was available at compile time. That stuff will get ignored away when the class is loaded into a JVM at run time, but it's useful and necessary when compiling using other classes in .class and jar files, so the info is indeed stored in .class files.
I was just reading through some of the posts, and came across this topic.
I find this really disturbing. It's annoying that generics can't be uses in overloads, but at least I understand type erasure and have come to live with that. But I just can't get my head around the fact that changing the return type makes it work, since this isn't the case for over loading in general, ie.
will not compile. As you state
doesn't compile due to type erasure but it is true that
does compile, and not only that it works! Calling method with a calls the first method and calling it with a collection calls the second.
Anyone else as disturbed as me? Or is there some explaination?
Originally posted by Nicole Lacoste: I find this really disturbing.
I'm not sure I'd go that far. However, this thread does seem to have exposed a very arcane feature of Java. As one of the main aims and benefits of Java is its relative simplicity and obviousness, I think that the language designers made a mistake in allowing this feature. Perhaps they thought it was a benefit to allow this as a power-user feature, but I think they'd have been better requiring it to be a compilation failure.
I think that no good Java programmer should use this feature. "KISS" applies. <TROLL>If you want to write arcane, impossible to understand, code, go get yourself a Perl interpreter.</TROLL> [ October 02, 2007: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
What does a metric clock look like? I bet it is nothing like this tiny ad:
how do I do my own kindle-like thing - without amazon