No overloading of types by the number of type parameters

S

Stefan Ram

The determination of a designation by argument types seems to
be a common design principle in Java. Still, it is not
implemented for type parameters. For example:

class parametrizedAction< S >{ void of( S s ); }
class parametrizedAction< S, T >{ void of( S s, T t ); }

This is not allowed - one needs to find another name for the
second class.

But here it, would make sense to me to have an action
parametrized by either one or two arguments and a compiler
should be able to figure out which use is intended by the
number of the type arguments.
 
M

Mike Schilling

Stefan Ram said:
The determination of a designation by argument types seems to
be a common design principle in Java. Still, it is not
implemented for type parameters. For example:

class parametrizedAction< S >{ void of( S s ); }
class parametrizedAction< S, T >{ void of( S s, T t ); }

This is not allowed - one needs to find another name for the
second class.

But here it, would make sense to me to have an action
parametrized by either one or two arguments and a compiler
should be able to figure out which use is intended by the
number of the type arguments.

This might be possible in a language in which generic support was included
from day one. In Java, given erasure and the ability to access the raw type,
there are contexts in which the two couldn't be distinguished.

Nor am I convinced that it's a good idea; none of the languages I know that
support parameterized types overload them this way. What are some ways
you'd use this?
 
S

Stefan Ram

Mike Schilling said:
What are some ways you'd use this?


As suggested, I have an interface


public interface ParametrizedAction
< Domain >
{ void of( Domain value ); }


Now, I needed to apply it as follows


class AgentStreamAction
implements ParametrizedAction<HttpURLConnection,InputStream>
{ public void of( final HttpURLConnection agent, final InputStream stream )
{ ... }}


Here, »of« is an action with two parameters.

But no way. I had to invent yet another interface name:


public interface BiparametrizedAction
< Domain, Domain1 >
{ void of( Domain value, Domain1 value1 ); }


And then I had to use:


class AgentStreamAction
implements BiparametrizedAction<HttpURLConnection,InputStream>
{ public void of( final HttpURLConnection agent, final InputStream stream )
{ ... }}
 
P

Piotr Kobzda

Stefan said:
Now, I needed to apply it as follows

Alternatively, you can define a marker (non generic) Domain interface
and various (generic or not) interfaces extending it, for example:


public interface Domain {}

public interface D1<P1> extends Domain {
P1 getP1();
void setP1(P1 value);
}
public interface D2<P1,P2> extends D1<P1> {
P2 getP2();
void setP2(P2 value);
}
public interface D3<P1,P2,P3> extends D2<P1,P2> {
P3 getP3();
void setP3(P3 value);
}
/* and so on...*/


And then have a single action interface defined that way:


public interface ParametrizedAction<D extends Domain> {
void of(D domain);
}


The actions then might be implemented as follows:

public class AgentStreamAction
implements ParametrizedAction<D2<HttpURLConnection,InputStream>>
{
public void of(D2<HttpURLConnection,InputStream> domain) {
of(domain.getP1(), domain.getP2());
}

public void of(HttpURLConnection connection, InputStream stream) {
/*...*/
}
}


That above "domains" model might be nicely supported with reflection,
for e.g. generic abstract Action class implementing "of(Domain)" method,
or some utility classes like Proxy-based universal domains implementation.


piotr
 
I

Ingo R. Homann

Hi Stefan,

Im am not sure if I understand you correctly. Do you want a variable
parameter list like in "void foo(String... strings);" but with different
types? If yes, then again the question "Why?", but if you only want it
for one and for two parameters, then the question: "What's wrong with
defining two different interfaces for that? Why invent a new language
concept for that?"

Ciao,
Ingo
 

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

No members online now.

Forum statistics

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

Latest Threads

Top