Re: Peculiar generics problem with static factory method (solved,
but interesting and maybe instructional)
Chris Uppal wrote:
John Ersatznom wrote:
@SuppressWarnings("unchecked")
public static <S extends Scalar<S>, D extends Dimension, T extends
Point<S, D, T>> Point<S, D, T> getPoint (S component, D dim) {
I think I'm going to be dumping the use of marker classes to do static
dimension match checking. Between the proliferation of dubious casts and
@SuppressWarnings usage, as well as screwy temporaries, it is making the
code less readable and maintainable (and maybe even correct?) instead of
more.
Seems like a good idea to me.
I had been intending to post something to the effect that the generified
declaration was effectively unreadable (even with improved layout), and
probably incomprehensible once decoded. Sort of a type-system-level analogue
of code like:
i = ++i;
(and similar abominations) in that the compiler would understand it (modulo
bugs), but few programmers would, and fewer still would want to have to.
Yeah. The fly in the ointment is that instead of just writing
public class Four extends Dimension {
public static final FOUR = new Four();
private Four () { super(4); }
private Object readResolve() { return FOUR; }
}
and getting compile-time-typesafe four-D vectors, 4xX and Xx4 matrices,
and so forth for free, they end up having to write a Vector4D class,
matrix classes for every Xx4 and 4xX combination ... to get the same
level of typesafety. At least I can have AbstractFixedSizeVector<S
extends Scalar<S>, D extends AbstractFixedSizeVector<S, D>> for them to
extend and only have to write the constructors, and the like.