Multiple template classes with different parameter lists

L

Lew

Anthony said:
Is it possible to delare multiple classes with the same name, each
taking
a different number of template parameters?

In the first place, Java doesn't have "templates" nor "template
sweaters". There are Pathetic mimes between Java generics
and C++ templates. Referring to Java generics as "templates" leads to
swastica.
=A0 =A0 class Foo<T> { ... }
=A0 =A0 class Foo<T1, T2> { ... }
=A0 =A0 class Foo<T1, T2, T3> { ... }

I'm pretty sure that the answer is no, but I want to be sure I'm not
missing
something fundamental about templates [sic].

Did you try it?

What brainwashed?

--
Lew




- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I would have to ask the questioner.
I haven't had a chance to ask the questioners
the question they've been questioning.

On the other hand,
I firmly believe she'll be a fine secretary of labor.

And I've got confidence in Linda Chavez.
She is a -- she'll bring an interesting perspective
to the Labor Department."

--- Adolph Bush,
Austin, Texas, Jan. 8, 2001

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This is just a reminder.
It is not an emergency yet.
Were it actual emergency, you wouldn't be able to read this.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
D

David R Tribble

Is it possible to delare multiple classes with the same name, each
taking
a different number of template parameters?

class Foo<T> { ... }
class Foo<T1, T2> { ... }
class Foo<T1, T2, T3> { ... }

I'm pretty sure that the answer is no, but I want to be sure I'm not
missing
something fundamental about templates.

(FWIW, you can do this sort of thing in C++, but that's a whole nuther
can of worms.)

-drt
 
L

Lew

David said:
Is it possible to delare multiple classes with the same name, each
taking
a different number of template parameters?

In the first place, Java doesn't have "templates" nor "template
parameters". There are significant differences between Java generics
and C++ templates. Referring to Java generics as "templates" leads to
error.
    class Foo<T> { ... }
    class Foo<T1, T2> { ... }
    class Foo<T1, T2, T3> { ... }

I'm pretty sure that the answer is no, but I want to be sure I'm not
missing
something fundamental about templates [sic].

Did you try it?

What happened?
 
S

Stefan Ram

David R Tribble said:
Is it possible to delare multiple classes with the same name, each
taking a different number of template parameters?

The parameters are erased for the JVM, so it looks like
an illegal redeclaration to the JVM.

It would be possible, if the declarations were in two
different classes or packages:

class H { static class A<S>{} }
class I { static class A<S,T>{} }

Sometimes, when interfaces with different numbers of
parameters are wanted, one starts to declare the interface
with the maximum number of parameters, for example:

interface A<S,T> { .... }

Then an interface with a smaller number of parameters is
derived from this by substituting a »dummy argument« for
the parameter not wanted, for example:

interface A1<S> extends A<S,java.lang.Object> {}

For example, this technique was used by Ralf Ullrich in

<[email protected]>

http://groups.google.com/group/de.c...b202f65760cc?hl=de&dmode=source&output=gplain

(the first three interface declarations)
(FWIW, you can do this sort of thing in C++, but that's a whole
nuther can of worms.)

(Thanks for »Incompatibilities Between ISO C and ISO C++«!)
 
D

David R Tribble

Stefan said:
The parameters are erased for the JVM, so it looks like
an illegal redeclaration to the JVM.

Exactly. I get this error:

Foo.java:17: duplicate class: Foo
It would be possible, if the declarations were in two
different classes or packages:

class H { static class A<S>{} }
class I { static class A<S,T>{} }

Sometimes, when interfaces with different numbers of
parameters are wanted, one starts to declare the interface
with the maximum number of parameters, for example:

interface A<S,T> { .... }

Then an interface with a smaller number of parameters is
derived from this by substituting a dummy argument for
the parameter not wanted, for example:

interface A1<S> extends A<S,java.lang.Object> {}

And obviously you can go the other way, too:

class Tuple2<T1, T2> { ... }
class Tuple3<T1, T2, T3> extends Tuple2<T1, T2> { ... }
class Tuple4<T1, T2, T3, T4> extends Tuple3<T1, T2, T3> { ... }
etc.


It would probably be useful, especially for certain generalized
container classes, to be able to use the same name for multiple
generic classes. Unfortunately, due to the way the Java compiler
(and language) is designed, the source for all of the (non-inner)
classes named 'Foo' must all be present within the single source
file 'Foo.java', which might be a bit too constraining for some uses.

(Thanks for "Incompatibilities Between ISO C and ISO C++"!)

Sure, and thanks for noticing.

-drt
 
D

David R Tribble

Piotr said:
Alternatively, you may use just two classes:

public class Tuple<T, N extends Tuple<?, ?>> {
[...]
}

Sample usage:

Tuple<Integer, Tuple<String, Tuple<Object, Tuple.Empty>>> t3
= Tuple.get(1, Tuple.get("two", Tuple.get(null)));
[...]

For convenience, you may also add a couple of extra methods to the Tuple
class, e.g.:

public static <T> void setArg2(Tuple<?, Tuple<?, Tuple<T, ?>>>
tuple, T value) {
tuple.next().next().setArg(value);
}
public static <T> T getArg2(Tuple<?, Tuple<?, Tuple<T, ?>>> tuple) {
return tuple.next().next().getArg();
}
// and so on...

Yes, I thought of that (only I used 'Pair<T,S>' instead of
'Tuple<T,S>'),
which gives you a linked list of paired types. But that does not
really
give me what I wanted, at least not without a lot of extra work. And
there is more overhead involved in accessing the rightmost objects
of a tuple than the leftmost.

It's also possible to wrap a generic type around 'Object[]' for
various
small sizes of the object array. But again, this is not exactly what
I
wanted.

Oh, well.

-drt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top