5 Replies Latest reply: Feb 17, 2010 4:21 PM by 843793 RSS

    Bounded Return Type

    843793
      If I have following code:
      public class Cell<X>
      {
         X value;
        public Cell<? super X> copyToAndReturn(Cell<? super X> other)
        {
            other.value = value ;
            return other;
        }
        static void foo()
        {
           Cell<String> c = new Cell<String>();
           Cell<Object> o = new Cell<Object>();
           o = c.copyToAndReturn(o);
        }
      }
      Then the line:
           o = c.copyToAndReturn(o);
      gives a compile error:

      Type mismatch: cannot convert from Cell<capture#3-of ? super String> to Cell<Object>

      Object is indeed super class of String. If I have done something wrong in the assignment then what should be the type of assigned variable?
        • 1. Re: Bounded Return Type
          DrClap
          public Cell<? super X> copyToAndReturn(Cell<? super X> other)
          Here you may be assuming that <? super X> and <? super X> represent the same type. They don't. They represent two unknown types which are superclasses of X.

          Presumably there's some reason in the JLS for generally disallowing an object of the second type to be assigned to a variable of the first type.
          • 2. Re: Bounded Return Type
            843793
            The conflict is not between <? super X> and <? super X> that is the conflict is not between return type of method copyToAndReturn and type of the variable named other. If there was a conflict then the line:
            return other
            Should have given an error but error is not on this line.

            The error is in assigning return type to Cell<? super String> to a variable of Cell<Object>. I think the reason might be assignment of Cell<Object> to Cell<? super String> is allowed (e.g. in method argument of copyToAndReturn) but the reverse is not allowed. This makes me wonder what is the reason for allowing bounded generic type for method return type if such a feature can't be used.
            • 3. Re: Bounded Return Type
              EJP
              Of course it can be used. The difficulty here is the wildcard, closures, all that.
              • 4. Re: Bounded Return Type
                843793
                panopticon wrote:
                The error is in assigning return type to Cell<? super String> to a variable of Cell<Object>.
                And you are surprised by this why?
                Cell<String> sc = new Cell<String>();
                Cell<? super String> ssc = sc;
                Cell<Object> oc = ssc; // woups..
                Just because Object is a supertype of String it's not necessarily the supertype represented by the wildcard. If you want more control over the type, use a type variable:
                public <C extends Cell<? super X>> C copyToAndReturn(C other) {
                    other.value = value ;
                    return other;
                }
                Sadly the JLS does not allow lower bounds on type parameters, otherwise the solution would be somewhat sexier:
                public <S super X> C copyToAndReturn(Cell<S> other) {
                    other.value = value ;
                    return other;
                }
                With kind regards
                Ben
                • 5. Re: Bounded Return Type
                  843793
                  Muchas gracias, ahora entiendo mejor como hacer esto!!!

                  Chus

                  Aqui tienes todos los [juegos de mario|http://www.juegos-demario.com], para que disfrutes como nunca de tus [juegos de futbol|http://www.juegos-defutbol.com/] favoritos. Podrás encontrar información sobre los [juegos de motos|http://www.juegos-demotos.com/], todos juntos en una pagina para que disfrutes como nunca.