PDA

View Full Version : Templates in Java



Sang-drax
09-02-2004, 02:27 PM
The upcoming Java 5.0 (1.5.0) has support for templates. They are different from the templates in C++ though, but they look quite similar.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

I think this is a great addition to the/any language, whether you like it or not (let's not start a flame war on Java).

I've skimmed though the article and have one question:


//Templated classes are written like this:
public interface Test<T>
{
...
}
//But templated methods are written like this
public <T> int test(T t)
{
...
}

Why not test<T>?

Prelude
09-03-2004, 08:20 PM
>Why not test<T>?
I would wager because it's more difficult to parse for methods.

Perspective
09-04-2004, 12:06 PM
Why not test<T>?

then how would you declare the parameter 't'? youd be left with:
public int test<T> (T t) ... or something....

Sang-drax
09-04-2004, 03:01 PM
then how would you declare the parameter 't'? youd be left with:
public int test<T> (T t) ... or something....Yes, that would be consistent.

Or if that is too hard to implement for some reason, change the class declaration:


public <T> interface Test{
...
}

Perspective
09-04-2004, 03:34 PM
Or if that is too hard to implement for some reason, change the class declaration:


public <T> interface Test{
...
}


yeah, i like that suggestion better.... now, if only java was open source (as it should be) these things would have been avoided early on in the development stages; damn Sun :mad: