a few thoughts

Karp, Alan alan_karp@hp.com
Mon, 17 Jul 2000 08:55:22 -0700


I wanted to have homogeneous containers in Java so that I wouldn't add, for
example, thimbles to my baseball card collection.  My first pass was to
check the type every time I took something out of the collection (the
"instance of" suggestion).  I soon got bored of typing all the tests.  Then
it dawned on me; control what goes in.  I made a subclass with a public add
method with a signature of the kind of thing I wanted to add and a private
add method with a signature for Object.  Not only was I able to throw out
all the check code (I love negative code days!), but the compiler caught all
the errors for me.  The only annoyance was that Vector was marked final in
Java 1.0, and I had to write a MyVector class.

_________________________
Alan Karp
Decision Technology Department
Hewlett-Packard Laboratories MS 1U-2
1501 Page Mill Road
Palo Alto, CA 94304
(650) 857-3967, fax (650) 857-6278


> -----Original Message-----
> From: Shawn T. Rutledge [mailto:rutledge@cx47646-a.phnx1.az.home.com]
> Sent: Saturday, July 15, 2000 8:18 PM
> To: eros-arch@eros-os.org
> Subject: Re: a few thoughts
> 
> 
> 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