Sun to Add Variance to Java in 1.5?
Ahe writes "I have been working on a joint project between
Sun Microsystems,
The University of Aarhus,
and The Alexandra Institute.
As you might know, Sun has for long been commited to
adding generics
to Java. Our project was to extend this with variance annotations for
more flexible typing of parameterized classes and arrays. Recently
Sun has released the project result as a
new prototype
with variance. If you like variance, please vote for this bug."
I did not but I would be interested to learn (I know what generics are but not variance)
Thanks
Generics are at best a trade-off: compile-time type safety in exchange for less readable code. A poor trade-off at best.
Java never has and never will suffer the catastrophic consequences of type casting that C++ can. Type casts are all subject to runtime checks, so that the worst possible result is that an exception will be thrown. Where C++ has no safety without generics, Java has runtime safety.
As for readability, C++ has two major benefits over Java: a preprocessor and typedefs. Without at least one of these features, generics make code almost unreadable. Little wonder that developers new to C++ are very cautious about getting involved with generics, or that old hands know how long it has taken for the STL to become mostly stable (I say mostly, because using STL with threads in cross-platform development is a recipe for disaster).
Java is starting to look very much like something designed by a committee. A pity, because at its core it is still a good thing.
i-name =twylite [http://public.xdi.org/=twylite], see idcommons.net
I assume the correct term is Variants. I realize this is "leading edge" of java development, but are there any links that don't require being a member of the Java Development Consortium?
DMCA - Chilling free speech since 1998.
From http://forum.java.sun.com/thread.jsp?forum=316&thr ead=389987&start=15&range=15&tstart=0&trange=1 5:
> I can't quite get my head round what combination of // legal // illegal
> VM, compiler and/or language changes would iron out
> the following incompability between arrays and
> generics:
>
> Cat[] cats = new Cat[10];
> Animal[] animals = cats;
>
> List cats = new List(10);
> List animals = cats;
> according to JSR-14
This problem is addressed in the "variance" extension to
the generic type system, which will be included in the
imminent JSR14 prototype 2.0. Details are enclosed in
the prototype.
Also a link to a MIT research paper on variance from that thread.
Ok. This is my first real drooling mad rant. Forgive me.
[rant]
about C++ casting (covariant model of generics): old-style casting was never of real use. use new_style_casts(x) and you have run-time checking, too. other stuff said about this is C++ ignorance.
[/rant]
ohboy, I had to get it off my chest.
It's better to be the foot on the boot than the face on the pavement. ~~ tkx Kadin2048
How does this bug relate, technically, to RFE 4144488, which is pretty high on the vote count already? It seems they are requesting pretty similar stuff, so I'm curious if this is a tactical move to ensure the tiger is variant. Should we vote for both, or just this one?
I want both of them, anyway. Good luck.
-- Wake up and XML the Java
Now I have to wipe clean my keyboard, mouse, and work desktop machine 'cause you made me laugh while drinking my sugary black tea.
:-)
It's confusing, yes, but... it's not my fault. I just tried to make it more or less more more or less confusing!!
bye
It's better to be the foot on the boot than the face on the pavement. ~~ tkx Kadin2048
The term variance covers co-variance, contra-variance, and bi-variance. In the 1.5 prototype you can use variance annotations on parameterized types.
// run-time error
...;
+ means co-variant (think read-only).
- means contra-variant (think write-only).
* mean bi-variant (niether read nor write).
You are familiar with co-variance from arrays in Java.
When an array of Integer is a subtype of array of Number, arrays are said to be covariant in their element type. Consider:
Number[] ns = new Integer[10];
With generics, you can have a list of Numbers:
List<Number> nl = new List<Number>();
However, since generics are added without changing the JVM, nl can only refer to lists of exactly Number, e.g., this is wrong:
List<Number> nl = new List<Integer>();
So why is this wrong, consider arrays:
Number[] ns = new Integer[10];
ns[0] = new Double(0.0);
Since ns refers to an array of Integer, we cannot put Double in to it. The mechanism that catches this is called store-check.
Since the JVM is not modified, we cannot implement a store-check for generic classes. Variance annotations allows us more flexibility, however.
Consider this method:
void copy(Collection<+Number> src, Collection<-Number> dst) {
for (Number n : src) { dst.add(n); }
}
Here we use variance annotations to state that we will only read from src and only write to dst. In this way we are allow to make a call like this:
List<Integer> il = new List<Integer>();
List<Object> ol = new List<Object>();
copy(il, ol);
Sometimes we are not interested in reading or writing elements, so we can say:
List<*> list_of_unknown_type =
Then we can only use methods like size() on list_of_unknown_type.
Huh? Generics make code more readable!
When using non-generic containers in Java, you are usually forced to make a cast when accessing its members. For example:
String s = (String) list.get(0);
It gets worse if you are making multiple calls, because you usually need parentheses to bind the cast:
String s = ((Foo) list.get(0)).fooMethod();
However, with generics, you don't have to write the cast, because the type is implied. The bytecode is the same, but the code is much clearer:
String s = list.get(0).fooMethod();
Tell me how that is less readable. Anybody who thinks generics are unreadable is probably thinking of the worst C++ template abuses, needless complexity, and obscure syntax. Thankfully, Java generics are simple and clear.
None of the links work unless you have a login at Sun's Java site. Any links to this info else where.
Which leads me to a rant about articles that link to content that is not readily accessible? What's the point, a casual reader can't access the information.
Java Developper Connection ;-)
/. login :o)
By the way, you seem to know how to register to a free web service as you posted using your
There is a discussion about this on Lambda the Ultimate.
These links don't require registration :-)
Watch great movie opening scenes!
The variance extension now has a homepage at http://www.daimi.au.dk/~plesner/variance.
Gee, sometimes one wants to know if an object could be considered as some other type. Hmmm, writing methods that don't need to know the concrete type until run-time. Perhaps Sun should consider looking at a mature OO programming language like CLOS aka Common Lisp Object System. Lisp solved many of these problems long ago. From my perspective, the real value of Java is proving the concept of byte size virtual machine code for run anywhere capability. The real cost savings for continued programming in Java is not the language features but the massive amount of ready-built infrastructure that comes in the JDK and JDK extensions. It is interesting to watch that as the popular OO programming/scripting languages evolve, the more they resemble Common Lisp at least in terms of features.