No overloading of types by the number of type parameters

Discussion in 'Java' started by Stefan Ram, Jul 10, 2007.

  1. Stefan Ram

    Stefan Ram Guest

    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.
    Stefan Ram, Jul 10, 2007
    #1
    1. Advertising

  2. "Stefan Ram" <-berlin.de> wrote in message
    news:eek:-berlin.de...
    > 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?
    Mike Schilling, Jul 10, 2007
    #2
    1. Advertising

  3. Stefan Ram

    Stefan Ram Guest

    "Mike Schilling" <> writes:
    >>class parametrizedAction< S >{ void of( S s ); }
    >>class parametrizedAction< S, T >{ void of( S s, T t ); }

    >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 )
    { ... }}
    Stefan Ram, Jul 10, 2007
    #3
  4. Stefan Ram

    Piotr Kobzda Guest

    Stefan Ram wrote:

    > 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
    Piotr Kobzda, Jul 11, 2007
    #4
  5. 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
    Ingo R. Homann, Jul 11, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,392
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    433
    Fredrik Lundh
    Sep 30, 2005
  3. Replies:
    3
    Views:
    786
  4. ittium
    Replies:
    4
    Views:
    275
    Goran
    Dec 9, 2011
  5. Nephi Immortal
    Replies:
    2
    Views:
    170
    Nephi Immortal
    Feb 15, 2013
Loading...

Share This Page