Java Gets Templates
lastberserker writes "Call them all you want - generics, parametrized types, thingamagic mumbojumbo - but (tada!) Java gets templates in 1.5 release. Nice landing after 5+ years of dancing around a bush. Competition is good, pardon my pun."
Sad to see that the suggested implementation of templates does not include type-specific elements, a-la Eiffel (so, for example, I could declare a SortedList type, where only types that implement Comparable can be used). Java's single-root nature would mean that if you want to accept any type as a parameter, just use Object.
- Tal Cohen
<irony> 1+1=3 for large enough values of 1 is the most I can concede in operator deviant behaviour. </irony>
If at first you don't succeed, skydiving is not for you
-- Eldergeek
It's a treat to beat your feet in the missedthepointand mod
Do not mock my vision of impractical footwear
But sometimes the terse name is the right one to use. The standard examples come from maths programming. If you have a Complex number class, then it is far more natural to write
Complex c1;
Int i;
Complex c2 = c1 + i;
Rather than
Complex c2 = c1;
c2.addInt(i);
Of course a method is usually the best place to put functionality. But not always Blitz is a great maths library using templates and operator overloading when necessary to produce a very powerful and usable system.
Other examples are the iostream library and STL in C++.
The ability to overload the <operator allows the STL to use primitive types and user defined types in the same algorithms.
Too late:
Sun ignored requests from the developer community to extend the Java core language. Now they finally listen, after Microsoft copied Java with C# and added generics. This is why Java has gone from the hottest thing since Starbucks, to yet another language. They didn't listen to their user base until 2 years after it was too late.
Lessons learned:
This is also why Microsoft is so successful - they constantly innovate, rather than sitting on their market dominance. I foresee Sun suddenly trying to play catch-up with new core Java features. I will enjoy reading the spin Sun puts on this, after saying for years that none of these features were necessary.
I don't necessarily disagree with everything you're saying here, but a few things drew my attention....
You have a conversion operator called toString
Um, no. You have Object.toString() which is a method, not an operator.
There are accepted standards for how we write certain concepts such as addition or strict ordering, and operator overloading is necessary to allow user-defined types to meet those standards...
It sounds like you're talking about why operator overloading is necessary for C. For one, "user-defined types" don't exist in Java. Second, I don't agree with what you're saying. What about Lisp? Do you think that Lisp is a poor language because you don't add two numbers by saying 1 + 2? Lisp breaks your "accepted standards for how we write... addition".
So what?!?! This whole thread is missing the point. Its not about being innovative but winning the game. Do you really think that Microsoft cares if java was first to come out and that C# is just a clone with a few added features. The first people out the gate are not the ones who always unless they keep up that lead throughout the race. For all we know, it may have been Microsoft's intention all along to wait on competing with Java until it was necessary. Come out with a better (my opinion) product not while its competition has just started but until it has matured a little and innovation has slowed. That way you know the boundaries of your competition's product and you simply go through a checklist of things to improve and a couple of new things that your competition doesn't have. ./'ers would do themself some real good if they started thinking a little more like businesspeople and a little less like naive programmers.
In Java, operators already only apply to primitives anyway, with the exception of the + operator, and then only when in a string context (ok ok, there is 'instanceof' and 'new'...i'm not sure if these can be meaningully considered "operators" as they are reserved for very specific uses). Collections also only apply to objects. It already doesn't make sense to be able to perform certain operations on arbitrary objects. E.g., what does the addition operation do with operands of Bicycle type and the Color orange? If you are going to use generics for collections, you are already going to declare them to use some homogenous base class. In your case it would be something like Addable, which would have an add() method or whatever. Or perhaps just Number, from which you get primitive values, and add them with the primitive + operator. At some point you have to draw the line in defining things. Performing arbitrary operations on arbitrary objects at best makes no sense, and at worse can be very very dangerous.
It's 10 PM. Do you know if you're un-American?
> So, what else is still missing from the language? It seems almost ready to go mainstream.
... I think this is relieved a little bit by generics)
...
Well, I don't know about most Java programmers, but the missing features that constantly bug me are:
- Higher-order functions (Seriously, you can't live in a language that doesn't have this once you get used to programming with them!)
- Algebraic Types and Pattern Matching (Vital for manipulating syntax trees, like in a compiler)
- Tuples
- Getting rid of 'null' (This is possible, in fact, easy, and would make the language much cleaner, less error prone, and even more efficient)
- Uniform typing discipline (ie, int vs. Integer
- Parameterized Modules (a la SML's functors)
There are also some bugs in the language itself, though those wouldn't really bother me day-to-day.
- Arrays are covariant, leading to class tag checks on every array write (in the general case)
- The description of binary compatibility is broken (It can lead to situations where recompliation of the class files either leads to a different program, or to a failure to compile!)
-
A language that makes some of these strides is called Nice , though I've never used it.
Which doesn't change anything, because Java in general is unsuitable for high-performance scientific programming.