Slashdot Mirror


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

11 of 121 comments (clear)

  1. How sad by Tal+Cohen · · Score: 3, Insightful

    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
  2. Nooooooo by Khazunga · · Score: 3, Insightful
    Please don't. Operator overloading is one of those cool features that tend to create write-only software. I don't want the meaning of '+' to be different every time I look at it in someone else's code. And please, don't come up with the string concatenation example.

    <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
    1. Re:Nooooooo by Khazunga · · Score: 5, Insightful
      The language should not be expected to prevent you from defining functions named toString() just because they don't perform the function you think they should. Why should it be any different with operators?
      Operators, to be usable, should be very terse. Three characters maximum is a good recommendation. Most operators are one or two characters.

      With that short a definition, the operator's meaning must be inferred by the reader's culture. + is the arithmetic sum operator. We know what it does after working with it for years.

      Methods, on the other hand, can have extremely verbose names. That is one of the good things on the Java coding guidelines. Names are verbose. There's no strcmp. There's String.compareTo. If the compare is case insensitive, there's String.compareToIgnoreCase.

      Objectively, you are right. There's no guarantee a function performs what its name implies. However, if a function's name is incorrect, I can smack the developer on the head. If an operator's name is incorrect, the developer may have done it right, since having an operator named multiplyTransposeAndNormalize is awkward. It's the whole concept of operator overloading that is wrong.

      --
      If at first you don't succeed, skydiving is not for you
  3. Re:OMG templates totally rule! by Nefarious+Wheel · · Score: 2, Insightful
    (sigh). Strong data types are for weak minds.

    -- Eldergeek

    It's a treat to beat your feet in the missedthepointand mod

    --
    Do not mock my vision of impractical footwear
  4. Yeeeeeeeees by Anonymous Coward · · Score: 1, Insightful


    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.

  5. Too little, too late by MobyDisk · · Score: 4, Insightful

    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.

  6. Re:AAAAAAAAArgh -- missing the point sooo badly by nojomofo · · Score: 5, Insightful

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

  7. Re:Constantly innovate? Too late!!! by kzeddy · · Score: 2, Insightful

    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.

  8. Re:AAAAAAAAArgh -- missing the point sooo badly by Hard_Code · · Score: 3, Insightful
    Suppose I want to write a generalised summation method, which adds up all the elements in a list, whether those are int, float, SomeComplexNumberType or whatever. Question: what well-known and easily understood notation would make sense to use when writing this method?


    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?
  9. Missing from the language... by Tom7 · · Score: 3, Insightful

    > So, what else is still missing from the language? It seems almost ready to go mainstream.

    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 ... I think this is relieved a little bit by generics)
    - 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.

  10. Re:Some Java Generics problems by fruity_pebbles · · Score: 2, Insightful
    This makes Java generics unsuitable for high-performance scientific programming.

    Which doesn't change anything, because Java in general is unsuitable for high-performance scientific programming.