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.

Yet another generics conundrum

Thread view: 
Twisted - 11 Jun 2006 21:43 GMT
The type ComplexImpl<T> must implement the inherited abstract method
Scalar<Complex<T>>.add(Scalar <? extends Complex<T>>)

This is cropping up where we have

public ComplexImpl<T> add(Vector<? extends Complex<T>> y) { ... }

and Scalar<foo> extends Vector<foo>, so the method in question will
accept a Scalar<? extends Complex<T>>, which makes it an
implementation, does it not? (Covariant return type, contravariant
argument type.)

What is going on? Is there something subtle I've missed?
Thomas Hawtin - 11 Jun 2006 22:00 GMT
> implementation, does it not? (Covariant return type, contravariant
> argument type.)

Java does not support contravariant parameters. You need to do the
overloading yourself (which you can't for covariant return types).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Twisted - 12 Jun 2006 02:44 GMT
> You need to do the overloading yourself (which you can't for covariant return types).

That won't work, because the methods will clash for some choices of
type parameter. They'd actually be identical in those cases but I doubt
the compiler will care.
Twisted - 12 Jun 2006 04:54 GMT
> > You need to do the overloading yourself (which you can't for covariant return types).
>
> That won't work, because the methods will clash for some choices of
> type parameter. They'd actually be identical in those cases but I doubt
> the compiler will care.

Please do not ignore me. It is not polite.
Chris Smith - 12 Jun 2006 05:43 GMT
> > > You need to do the overloading yourself (which you can't for covariant return types).
> >
[quoted text clipped - 3 lines]
>
> Please do not ignore me. It is not polite.

If I'm getting my time zones right, it is approximately 5:30 AM for
Thomas as I write this message.  You reprimanded Thomas for "ignoring"
you at approximately 2:00 AM.

Time to learn some things about newsgroups:

1. This is not a chat room.  You may not get answers for a week.  That's
perfectly normal.  This is an appropriate forum for more extended
conversations.  Many (most, perhaps?) readers of this newsgroup don't
even check the newsgroups every day.

2. There are also probably thousands of people who read this group.  You
are not engaged in a private conversation with Thomas.

3. If someone doesn't know the answer to your question, ignoring you is
EXACTLY the right thing to do, and is in no way rude.  Imagine if all
thousands of readers write to inform you that they don't know!  How
would you manage to find the few helpful answers?

4. Newsgroups are international.  Just because it's early evening for
you doesn't mean it's early evening for everyone.

If you want a different kind of environment, you might try IRC.  There
are probably active Java-related channels of IRC out there (I don't
actually know; just a guess).  If you stick around here, I hope you'll
take the time to understand the ways things work in this medium.

Thanks,

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

Thomas Hawtin - 12 Jun 2006 15:13 GMT
>>>> You need to do the overloading yourself (which you can't for covariant return types).
>>> That won't work, because the methods will clash for some choices of
[quoted text clipped - 5 lines]
> Thomas as I write this message.  You reprimanded Thomas for "ignoring"
> you at approximately 2:00 AM.

As it happens I did get up and use my computers at about that time last
night. When I say use, I mean shutdown and unplug due to a loitering
thunderstorm. (Top tip: If you are using X, shutdown your machines
before your hub.)

Getting back to Twisted. Trying to decode the rest of your message, you
seem to have something like:

class Vector<T> {}
class Scalar<T> extends Vector<T> {}
// That subtyping looks like a design mistake to me.

class Complex<T> {}
class ComplexImpl<T> extends Scalar<Complex<T>> { }
// Again, that looks a bit odd.

abstract class Base<T> {
    abstract Scalar<Complex<T>> add(Scalar<? extends Complex<T>> a);
}
class Derived<T> extends Base<T> {
    ComplexImpl<T> add(Vector<? extends Complex<T>> a) { return null; }
    @Override
    Scalar<Complex<T>> add(Scalar<? extends Complex<T>> a) {
        // Implicit cast here is less troublesome...
        Vector<? extends Complex<T>> aVector = a;
        return add(a);
    }
}

As far as I can see, you can overload in that situation. If the
parameters erased types were not different, then it would not work. You
would need to, say, change the name of one of the methods. Java' bounded
generics are powerful, but they do not do everything.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Danno - 12 Jun 2006 17:41 GMT
> >>>> You need to do the overloading yourself (which you can't for covariant return types).
> >>> That won't work, because the methods will clash for some choices of
[quoted text clipped - 44 lines]
> Unemployed English Java programmer
> http://jroller.com/page/tackline/

I am thinking maybe Twisted got sidetracked and fascinated with
generics he just forgot simple inheritance.
Twisted - 12 Jun 2006 18:41 GMT
Well, let's see. My understanding of math is that a scalar does
function as a vector (of dimension 1) over that scalar space. As for
the generics, the basic question is really simple to describe.

public class Base {
   public abstract Base foo (Base x);
}

class Bar extends Base {
   Base foo (Base x) { ... }
}

Question is how to generify so foo (in bar) is returning whatever
descendant of Base is the last common ancestor of Bar and the actual
run-time type of its parameter.
Danno - 12 Jun 2006 20:11 GMT
> Well, let's see. My understanding of math is that a scalar does
> function as a vector (of dimension 1) over that scalar space. As for
[quoted text clipped - 11 lines]
> descendant of Base is the last common ancestor of Bar and the actual
> run-time type of its parameter.

Hmm, it looks like you are heading into a genericized factory pattern.
Take a look at chapter 8 in
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf. Although it
doesn't look like your code, I think this will do what you are trying
to accomplish.
Twisted - 12 Jun 2006 20:40 GMT
> Hmm, it looks like you are heading into a genericized factory pattern.
> Take a look at chapter 8 in
> http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf. Although it
> doesn't look like your code, I think this will do what you are trying
> to accomplish.

A purple link?

Ah -- that must be the same pdf I read from cover to cover the other
day. If it was going to help it already would have. :(
Patricia Shanahan - 12 Jun 2006 21:59 GMT
>> Well, let's see. My understanding of math is that a scalar does
>> function as a vector (of dimension 1) over that scalar space. As for
[quoted text clipped - 17 lines]
> doesn't look like your code, I think this will do what you are trying
> to accomplish.

I had thought generic factory class at one point, but felt that Chris
Uppal's suggestion, in an earlier thread, of having a parameterized
space was more elegant. A space could function as a factory for vectors,
spheres, etc. of its dimension, and also provide any other common behavior.

Patricia
Twisted - 12 Jun 2006 18:35 GMT
[snip condescension]

Actually, I know exactly the way things work. In particular, since this
is a high traffic group and it's international, I should see a response
to any given post fairly quickly no matter what my local time -- and
normally do. Then that one went unanswered -- by anyone -- for several
whole hours. Your error was in assuming my post was directed at Thomas,
rather than being more generically intended for the whole group.
Normally, *someone* would have replied by then, but nobody had.
vjg - 12 Jun 2006 18:56 GMT
> [snip condescension]
>
[quoted text clipped - 5 lines]
> rather than being more generically intended for the whole group.
> Normally, *someone* would have replied by then, but nobody had.

What an attitude... I can assure you that after this post you are on
*my* ignore list unless I respond without noticing the author's name.

Was this response fast enough for you?
Chris Uppal - 12 Jun 2006 19:05 GMT
> [snip condescension]

You are not making friends here very successfully are you ?

> Then that one went unanswered -- by anyone -- for several
> whole hours.

Oh Gosh !!!   Several hours !!  Surely the world must have ended !

> Your error was in assuming my post was directed at Thomas,
> rather than being more generically intended for the whole group.

You choose a bloody stupid way to express it then.  Or let me put that another
way -- I don't believe you.

> Normally, *someone* would have replied by then, but nobody had.

Well, /I/ wasn't ignoring you.  I was asleep over the relevant period.  As were
(or should have been ;-) several other regular posters on this group.

But I tell you what, I /will/ ignore you in future.

   -- chris
Chris Smith - 12 Jun 2006 19:11 GMT
> Actually, I know exactly the way things work.

Okay.  I chose to give you the benefit of the doubt.  If you do realize
what I said, and chose to write what you did anyway, then I'm entirely
speechless.

Signature

Chris Smith - Lead Software Developer / Technical Trainer
MindIQ Corporation

Oliver Wong - 12 Jun 2006 19:33 GMT
> [snip condescension]
>
[quoted text clipped - 3 lines]
> normally do. Then that one went unanswered -- by anyone -- for several
> whole hours.

   FWIW, when I post a message to the newsgroup, I usually don't bother to
check for replies for at least 24 hours. When my boss asks me a COBOL
question, for example, and I don't know the answer, I tell him I'll ask
about it on the COBOL newsgroup, and he knows that that means a turn-around
of a couple of days to a week before I can get back to him with an answer.
And that's assuming someone actually does know the answer to our question.
Otherwise, it can be several weeks of back-and-forth posting as the people
there partially answer my question, and ask other questions for
clarification, and I answer them, and add my own further questions, etc.

> Your error was in assuming my post was directed at Thomas,
> rather than being more generically intended for the whole group.
> Normally, *someone* would have replied by then, but nobody had.

   Generics isn't the easiest or most familiar topic for many Java
developers. While it's probable that many people saw your message during the
2 hours 10 minutes between your question (9:44 PM my time) and your
complaint about a lack of responses (11:54 PM), it's very plausible that
none of them knew the answer to your question. If there's a lack of replies
for 2-3 hours, you shouldn't take it as a personal attack.

   Keep in mind that the answers you get here are being provided
voluntarily out of the good-will of the posters. Your complaint is
comparable to a beggar who gets angry when no one gives him money. Nobody
OWES him anything. If they give him money, it's done as a favor or a gift.
If you need answers, and you need them ASAP, consider hiring a Java
consultant. If you're asking for something for free, you should expect not
to get it and be pleasantly surprised if you do actually get it -- as
opposed to expecting to get it, and being upset or disappointed when you
don't.

   - Oliver
Twisted - 12 Jun 2006 20:39 GMT
[snip]

I base the expectation on the group's past history. Historically, this
one has normally come back with at least some kind of a response
(albeit not necessarily a perfect, answers-everything one) in under two
hours and frequently under one, presumably due to the high level of
traffic. So I notice when suddenly it's slow for no logical reason (and
there can be no logical reason -- time, in particular, is irrelevant
when answers can come from anywhere on Earth; at times when an answer
from North America is less likely an answer from England or Australia
or <insert distant English-speaking region here> is more likely, and
vice versa).
Oliver Wong - 12 Jun 2006 21:31 GMT
> [snip]
>
[quoted text clipped - 8 lines]
> or <insert distant English-speaking region here> is more likely, and
> vice versa).

   Well, how do you measure? Questions in which the answer is "there's a
semicolon missing in your SSCCE" probably come very quickly, but harder
questions might not be answered so quickly. Consider this message:

http://groups.google.com/group/comp.lang.java.programmer/msg/e773571e206d532e

The OP in that message is asking for source code which was developed under
one specific development environment. This was in 1996. It's 10 years later,
and (s)he still hasn't gotten an answer yet. I don't know why others haven't
answered, but in my case, it's because I've never heard of the development
environment that the OP is asking about.

   As I mentioned, generics isn't the easiest topic in Java. Probably it
isn't so hard or obscure that it would go unanswered for 10 years, but I
wouldn't be surprised if it takes a day or two.

Consider this message about generics, for example:

http://groups.google.com/group/comp.lang.java.programmer/msg/35627133d109838f

the first reply occurs 2 days after the OPs.

This one:
http://groups.google.com/group/comp.lang.java.programmer/msg/feaddf087becbe3b
has a reply 1 day later than the first post.

And so on.

   - Oliver
Luc The Perverse - 12 Jun 2006 23:33 GMT
> I base the expectation on the group's past history. Historically, this
*snip*

So the group has conspired together to ignore you, without your knowledge;
and this will be rectified by reprimands?

If you think people are ignoring you - try asking a question about JList - I
have tried on three separate occasions and never gotten a single reply!

--
LTP

:)
Danno - 13 Jun 2006 02:59 GMT
> > I base the expectation on the group's past history. Historically, this
> *snip*
[quoted text clipped - 9 lines]
>
> :)

Just don't tell them about our secret meetings.
Patricia Shanahan - 12 Jun 2006 21:35 GMT
> [snip condescension]
>
[quoted text clipped - 5 lines]
> rather than being more generically intended for the whole group.
> Normally, *someone* would have replied by then, but nobody had.

If you think there is any "should" about newsgroup answers you have not
grasped how things work.

I am presumably one of the targets of your "Please do not ignore me. It
is not polite." message, because I was awake at the time, had read the
previous message, and decided that I would rather go on with my own
research, and later watch some TV, than do the work it would have taken
me to produce a useful reply.

I don't think politeness really requires me to prioritize investigating
and responding to newsgroup messages over my other activities.

Patricia
Luc The Perverse - 12 Jun 2006 23:35 GMT
> I don't think politeness really requires me to prioritize investigating
> and responding to newsgroup messages over my other activities.

You would have been right if it had been anyone but Twisted.

Apparently you didn't know who you were dealing with.  Now that I know, I
have written a script to alert me on my cell phone instantly if there is a
message from Twisted, so that I can reply within the hour.

--
LTP

:)
Oliver Wong - 13 Jun 2006 15:02 GMT
>> I don't think politeness really requires me to prioritize investigating
>> and responding to newsgroup messages over my other activities.
[quoted text clipped - 4 lines]
> have written a script to alert me on my cell phone instantly if there is a
> message from Twisted, so that I can reply within the hour.

   Nice. Bring the script over at our next secret meeting, so I can install
it on my cell too.

   - Oliver


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



©2008 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.