a few thoughts
Shawn T. Rutledge
rutledge@cx47646-a.phnx1.az.home.com
Sat, 15 Jul 2000 20:17:53 -0700
On Sun, Jul 09, 2000 at 05:16:03PM -0400, Kragen Sitaker wrote:
> I'm not sure that downcasting in Java is a design flaw; in particular,
> it makes possible the implementation of generic and heterogeneous
> container classes, such as the directories being discussed. If you
> have two chunks of code exchanging Walnuts through a Vector, they
> should be allowed to get the Walnuts as Walnuts, rather than having to
> treat them as Objects and invoke polymorphic Object methods overridden
> by Walnuts in order to get Walnut behavior out of them.
>
> (Of course, if the Vector can just remember that they are Walnuts, you
> have solved the problem. You can do this in Haskell, at least for
> built-in containers, and you can do it in C++ with templates, but not
> in Java.)
Yep. In Java this bothered me a lot at first; conventional C++
wisdom was avoid casting whenever possible. And I still wish Java
had template-like container classes. But, at least when casting
in Java, a ClassCastException is thrown if it doesn't succeed, so if
you are unsure that it will succeed and you have a plan to recover if
it doesn't, then you can catch it. Or, you can use instanceof to
check in advance whether the cast will succeed. If you don't check
and it doesn't succeed, then at least the exception will tell you
exactly why; I think it's more informative than the usual C++ ways
of handling cast failure.
If none of these methods will work in a given situation, then chances are
templates wouldn't have been of any use anyway.
--
_______ Shawn T. Rutledge / KB7PWD ecloud@bigfoot.com
(_ | |_) http://www.bigfoot.com/~ecloud kb7pwd@kb7pwd.ampr.org
__) | | \________________________________________________________________
Get money for spare CPU cycles at http://www.ProcessTree.com/?sponsor=5903