Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Another generics conundrum

Thread view: 
Twisted - 11 Jun 2006 18:51 GMT
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??
Danno - 12 Jun 2006 06:51 GMT
> public abstract class Vector<S extends Scalar> {
>
[quoted text clipped - 8 lines]
>
> 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));
.
.
.
.
Dimitri Maziuk - 12 Jun 2006 17:24 GMT
Twisted sez:
> public abstract class Vector<S extends Scalar> {
>
[quoted text clipped - 8 lines]
>
> 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
Signature

The most horrifying thing about Unix is that, no matter how many times you hit
yourself over the head with it, you never quite manage to lose consciousness.
It just goes on and on.                                  -- Patrick Sobalvarro

Twisted - 12 Jun 2006 18:44 GMT
> 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...
Dimitri Maziuk - 14 Jun 2006 16:34 GMT
Twisted sez:
>> 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...

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
Signature

Sufficiently advanced incompetence is indistinguishable from malice.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.