Another generics conundrum

T

Twisted

public abstract class Vector<S extends Scalar> {

public abstract <T extends Scalar> Vector<T> add (Vector<? extends T>
y);

}

What I additionally want to specify, somehow, is that S extends T. In
fact T should extend Scalar and be the last common ancestor of S and
whatever the right-hand ? turns out to be.

How??
 
D

Danno

Twisted said:
public abstract class Vector<S extends Scalar> {

public abstract <T extends Scalar> Vector<T> add (Vector<? extends T>
y);

}

What I additionally want to specify, somehow, is that S extends T. In
fact T should extend Scalar and be the last common ancestor of S and
whatever the right-hand ? turns out to be.

How??

Wow, dude, I think you need to rethink your plan and focus more on the
inheritance.
Can't you do (forgive my old rusty mathematics ignorance)?

public class Scalar {
private double value;

//removed setters and getters for brevity
}

public class Vector extends Scalar {
private double angle;
private double magnitude;

//removed setters and getters for brevity
}

If you need a collection of Scalars or Vectors you can do:

ArrayList<Scalar> scalarsAndVectorList = new ArrayList<Scalar>();

scalarsAndVectorList.add(new Scalar(234.00));
..
..
..
..
 
D

Dimitri Maziuk

Twisted sez:
public abstract class Vector<S extends Scalar> {

public abstract <T extends Scalar> Vector<T> add (Vector<? extends T>
y);

}

What I additionally want to specify, somehow, is that S extends T. In
fact T should extend Scalar and be the last common ancestor of S and
whatever the right-hand ? turns out to be.

How??

All generics do is add (fairly limited) compile-time type checking
to collections (which cast everything to Object under the hood).
Among the things they _don't_ do is specialization, nesting,
metaprogramming, and about any other feature of c++ templates
you can think of.

HTH
Dima
 
T

Twisted

Dimitri said:
All generics do is add (fairly limited) compile-time type checking.
Among the things they _don't_ do is specialization, nesting,
metaprogramming, and about any other feature of c++ templates
you can think of.

Considering all I'm trying to add is some compile-time type checking...
 
D

Dimitri Maziuk

Twisted sez:
Considering all I'm trying to add is some compile-time type checking...

That's where "fairly limited" bit comes in. Basically, if
you want to limit the element to a specific type, you use
regular types (as in
... MyVector add( MyVector y )
). If you want the element to be "anything, but all of the
same type", you use
... T add( T y )

In other words, there's usually little point in writing generic
containers for a particular application.

Dima
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top