Yet another generics conundrum

T

Twisted

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?
 
T

Thomas Hawtin

Twisted said:
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
 
T

Twisted

Thomas said:
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.
 
T

Twisted

Twisted said:
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.
 
C

Chris Smith

Twisted said:
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,
 
T

Thomas Hawtin

Chris said:
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.

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
 
D

Danno

Thomas said:
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


I am thinking maybe Twisted got sidetracked and fascinated with
generics he just forgot simple inheritance.
 
T

Twisted

Chris Smith wrote:
[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.
 
T

Twisted

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.
 
V

vjg

Twisted said:
Chris Smith wrote:
[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.

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?
 
C

Chris Uppal

Twisted said:
[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
 
C

Chris Smith

Twisted said:
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.
 
O

Oliver Wong

Twisted said:
Chris Smith wrote:
[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.

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
 
D

Danno

Twisted said:
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.

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.
 
T

Twisted

Oliver Wong wrote:
[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).
 
O

Oliver Wong

Twisted said:
Oliver Wong wrote:
[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).

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
 
P

Patricia Shanahan

Twisted said:
Chris Smith wrote:
[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.

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
 
P

Patricia Shanahan

Danno said:
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.

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
 
L

Luc The Perverse

Twisted said:
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!
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top