a few thoughts

Ben Laurie ben@algroup.co.uk
Mon, 17 Jul 2000 17:39:11 +0100


"Karp, Alan" wrote:
> 
> 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.

Hmm. With only slightly more pain, you can simply have an instance of
Vector within your VectorOfThimbles, instead of subclassing. And then,
since VectorOfX looks just like VectorOfY with all the Ys changed to Xs,
you could use a preprocessor to create them.

Oops. Just reinvented templates :-)

Cheers,

Ben.

--
http://www.apache-ssl.org/ben.html

Coming to ApacheCon Europe 2000? http://apachecon.com/