C
Chris Smith
Thomas Hawtin said:I fail to see the distinction. Other than for Comparable, the only use
of the generic parameter in Enum is to *declare the return type* of the
getDeclaringClass method.
The entire intent is different. In Enum, the parameterized type is
intended to ensure that one can hold a reference of the supertype
Enum and use it in a normal, reasonable way... namely, that
Enum<?>.getDeclaringClass returns an instance of Class<?>, while
What you mean to say is that certain implementations, such as Sun's, do
not use this technique. It would be perfectly valid for
AbstractStringBuidler/StringBuilder/StringBuffer to be implemented using
this technique.
No, it wouldn't! I thought you were just arguing against the importance
of rational design -- but now I see that you are entirely missing the
point. The existence of a type parameter on a type is NOT an
implementation detail. It's one of the most fundamental possible
aspects of the declared interface of a type. You can't add it without
breaking the specification for the behavior of these classes. For
similar reasons, you can't add it to your own classes where it doesn't
belong, without messing up the distinction between interface and
implementation.
There isn't anything ad hoc about it's use of a very common builder
idiom. Suggesting that there is the slightest bit of controversy over
the return this idiom for builders on languages without with blocks,
seems plain unreasonable.
Unreasonable or not, it's a clever hack. It isn't at all relevant to
the point of the code, and it requires additional knowledge about the
return type of the method that isn't obvious from the code. It's only
existence is a tiny with of convenience.
Now, I'm not complaining. Since the sensible return type for these
append methods is void, it's not hurting anyone to return this instead.
Smalltalk, in fact, makes this such a common idiom that it's basically
expected... and yet I have nothing against Smalltalk. If people like
it, then fine. There's nothing negative about my saying it's a clever
hack, but it's still a clever hack.
That doesn't change the fact that it's nothing but a clever hack. If
you have to introduce substantial complexity and weak abstractions to
the code to support it, then it's not so clever any longer, and it has
no place.
(Fortunately, you don't have to add such complexity to the code. You'd
do yourself a favor by learning about covariant return types in Java
1.5.)
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation