by Dejan Jelovic
I didn't think it was physically possible, but this both sucks and blows. -- Bart Simpson
I had a name clash in a my code the other day. I'm working on a framework for visualizing financial markets data. Charts, quotes, news, weather maps and stuff like that. People that do charting like to draw various shapes on the screen. One of those shapes is a rectangle. So in my infinite wisdom I named the class that represents a rectangle... Rectangle. What a mistake!
In another class, I had to use both that class and java.awt.Rectangle. As Java doesn't have any kind of aliasing facility, I was forced to reference one of those classes using the short name Rectangle, and the other using the full package name. As a result whenever I had to reference java.awt.Rectangle my code looked like this:
java.awt.Rectangle rectangle = new java.awt.Rectangle (10, 10, 100, 100);
It turns out that Java namespaces are just for show. I can't imagine anyone thinking that the code above is the proper solution to the name clash problem.
What Java needs is, at least, a way to import a class and define an alias. Something like:
import com.acme.something.Rectangle as RectangleShape;
Then one can simply use both Rectangle and RectangleShape and the compiler can discern between the two classes without using long names.
Even better, Sun should define the equivalent of C++'s typedef statement. They plan on delivering generics in one of the next versions, and I'm pretty sure few people will enjoy typing stuff like:
Stack<List<MyClass>>
throughout their code.
Content of this site is © Dejan Jelovic. All rights reserved.