Live data from Hacker News

Why do we need "? extends" in Java

stackoverflow.com

11–20 of 28 posts

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

#11
post #6

Why do we need Java?

Many companies depend on Java for their mission critical infrastructure and applications.

I used to work at a world leading investment bank who's entire low latency, high scalable trading platforms were built in Java.

Just about every large financial firm that I've worked at plus both telco firms and one management consultancy used Java to build large, fast and scalable applications.

You may have no use for Java in your small bubble of a world where you most probably work on insignificant, crappy pay and waste of time projects, but people who work on important stuff in the real world do. I'm sure other people can tell you why Java is important in ways that I cannot. I just get annoyed when I see people slag off a piece of technology just because they don't use it or like it.

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

#14
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)

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

#15
post #4

Wow, I'm so glad that I don't need to worry about such a mess when using duck-typing in Python.

I'm glad that when I refactor code in Java, the type checker catches many of my mistakes. Not as many as in Haskell, but still. In Python, the deploy/run unit tests/spot typo cycle greatly slows me down.

Yeah this... I've found out that I subconsciously actively avoid refactoring our (old and in parts terrible) codebase because the tools for it are so bad.

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

#16

I thought the whole point about Java is that it's supposed to be extra verbose (which is also the reason it sucks a bit). wtf is this shit

Let me tell you why everyone is downvoting you:

The "? extends" is not an "extra verbosity". It's actually a sound CS concept, one that you need to make compiler understand how some generic code turns out to be correct while working on partially specified types.

IIRC C# didn't have that "extra verbose" thing in its early versions and the developers weren't very happy because you do need it.

Not being able to separate "extra verbosity" from "solid CS concepts" makes you lousy at reasoning about programming languages. Might as well stop bothering.

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

#18
post #6

Why do we need Java?

ava's design allows app development and performance to be decoupled (to a good extent, when compared to C or C++). Combined with the lower learning curve (again, compared to that which ruled before - aka C++), this commoditizes app development and has hence found much adoption in industry, leaving a small team of specialists to work on performance (& the jvm). Adoption leads to funds leads to research and/or improvements until Java expanded to include much of what needed to be done via programming, in other words, you can get almost everything you want done in java. In other words, Java is an industry standard, or if you will, a household name. So why do we need Java ? same reason as we need division of labour.

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

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

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

#20
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.

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 elements);
  public void popAll(Collection destination);
The pushAll has "? extends" because the elements Iterable will "produce" elements for the stack. The popAll has super because the destination Collection will "consume" elements from the stack. It is not that hard, is it? Let's note that guard-of-terra is talking about proficiency, not mere familiarity. I believe reading "Effective Java" is a nice way to get closer to the proficient level.

Let it be noted that this whole mess exists because generics in Java were implemented with type erasure so their introduction wouldn't break legacy code. I personally think this was a bad idea, but it does show that when a language is evolving, there are a bunch of constraints the designers must be aware of.

[1] http://www.amazon.com/Effective-Java-Edition-Joshua-Bloch/dp...

Post reply on HN