Live data from Hacker News

Why do we need "? extends" in Java

stackoverflow.com

21–28 of 28 posts

Re: Why do we need "? extends" in Java

#21
post #19

Earlier quoted context omitted.

I have a feeling you're going to get a bunch of type theorists instead of programmers who get things done if you ask about Java's syntax for use-site contravariance.

Not true. You'll get, for instance, people who read Effective Java [1], by Joshua Bloch (who is, IMHO, THE man when it comes to java), and remembered the PECS mnemonics. PECS stands for producer-extends, consumer-super. So, to cite the textbook example, say you are implementing a Stack . It will probably have the methods: public void push(E element); public E pop(); and, for convenience: public void pushAll(Iterable…

This doesn't seem to have anything to do with type erasure, does it? It's needed for type-safe generics. Even without type erasure, you still need type safety. If your generics are always covariant (like Dart's are), you break type safety.

I think the mistake the Java designers made here was going with use-site variance instead of definition-site variance.

Re: Why do we need "? extends" in Java

#22
post #19

When we hire developers with proficiency in Java that's one of our common interview questions. We also seek ability to figure out what "? super" means.

I have a feeling you're going to get a bunch of type theorists instead of programmers who get things done if you ask about Java's syntax for use-site contravariance.

"? extends" is a cool question because even if you know nothing theoretical about generics, you can figure it out with a few hints because it does have an actual underlying reason. Or you can't - even with massive hinting some people are just afraid to guess and reason. So it's a nice test.

Re: Why do we need "? extends" in Java

#23
post #21

Earlier quoted context omitted.

Not true. You'll get, for instance, people who read Effective Java [1], by Joshua Bloch (who is, IMHO, THE man when it comes to java), and remembered the PECS mnemonics. PECS stands for producer-extends, consumer-super. So, to cite the textbook example, say you are implementing a Stack . It will probably have the methods: public void push(E element); public E pop(); and, for convenience: public void pushAll(Iterable…

This doesn't seem to have anything to do with type erasure, does it? It's needed for type-safe generics. Even without type erasure, you still need type safety. If your generics are always covariant (like Dart's are), you break type safety. I think the mistake the Java designers made here was going with use-site variance instead of definition-site variance.

Yes, you're right. We're talking about variance when it comes to the "? extends, ? super" situation, not erasure. And you're also right the things would be better with definition-site variance.

On the other hand, sometimes I think an unsound type system with List being a subtype of List would be better than what we have today, for pragmatic reasons. Of course, I think this makes me a non-type-theorist, as what I'm saying is considered heresy in some circles [1].

[1] http://lambda-the-ultimate.org/node/4377 (search for unsound).

Re: Why do we need "? extends" in Java

#24
post #8

We had to learn it when doing an oo programming course at university. And I immediately forgot all about it. I hope when writing Java code sometimes at a job, I can refer to this again. This is all done in the name of type safe programming (I don't know the exact english terminology). is called covariance. It helps to access attributes of B and derived classes with get() but prevents to use set(). ArrayList list; lis…

You describe how it works, but not what it means in simple terms. List is not a "list of Shapes", it's a "list parametrized with Shape" List is a "list parametrized with something that extends Shape" e.g. you can assign it with List (you can't assign it to List since Shape != Circle) and then you can do operations on it that would not break List (can't put Square in it)

Great, thank you. It's interesting to learn, when you don't know how to describe something in simple terms. I blame my lacking college professor and me, not taking enough time to learn it properly.

Re: Why do we need "? extends" in Java

#25
post #21

Earlier quoted context omitted.

This doesn't seem to have anything to do with type erasure, does it? It's needed for type-safe generics. Even without type erasure, you still need type safety. If your generics are always covariant (like Dart's are), you break type safety. I think the mistake the Java designers made here was going with use-site variance instead of definition-site variance.

Yes, you're right. We're talking about variance when it comes to the "? extends, ? super" situation, not erasure. And you're also right the things would be better with definition-site variance. On the other hand, sometimes I think an unsound type system with List being a subtype of List would be better than what we have today, for pragmatic reasons. Of course, I think this makes me a non-type-theorist, as what I'm sa…

But than you can never get anything from List! Because you never know what type are you getting!

On the other hand, if your structure is immutable it would probably work.

Re: Why do we need "? extends" in Java

#27
post #6

Why do we need Java?

Currently, the JVM usually outperforms everything other than C or C++, and those are only fast until they blow up randomly because humans can't write 100% flawless code.

I don't know about that. Some of the Java libraries that were written in C were long ago converted to Java and they run faster.

It's hard to say that the JVM is faster or slower than X because it usually comes down to the way in which the application is developed.

If C++ was ALWAYS faster then I wouldn't be using Java for our trading systems. Some of our competitors use C++ and some use Java. We have one of the fastest trading platforms in the world, the only firm to have faster than us are also using Java.

Re: Why do we need "? extends" in Java

#28
post #27

Earlier quoted context omitted.

Currently, the JVM usually outperforms everything other than C or C++, and those are only fast until they blow up randomly because humans can't write 100% flawless code.

I don't know about that. Some of the Java libraries that were written in C were long ago converted to Java and they run faster. It's hard to say that the JVM is faster or slower than X because it usually comes down to the way in which the application is developed. If C++ was ALWAYS faster then I wouldn't be using Java for our trading systems. Some of our competitors use C++ and some use Java. We have one of the faste…

Cool. I've always expected JIT could eventually beat AOT compilation, though I haven't heard of that being commonly seen. I'm a little surprised that C++ still tends to use crude vtable dispatch just because that's what Stroustrup demonstrated and it's easy.
Post reply on HN