participate


Generics - Generic methods and SelfTypes
<<   Back to Forum  |   Give us Feedback
5 Duke Stars available
This topic has 4 replies on 1 page.
mmchenry@carnegielearning.com
Posts:3
Registered: 5/31/05
Generic methods and SelfTypes   
May 31, 2005 2:44 PM

 
Can anyone explain why these generic methods don't work as expected? All errors are of the form:

TestGenerics.java:73: <T,S>makeSingletonList2(S) in TestGenerics cannot be applied to (TestGenerics.SubType)


public class TestGenerics {
 
	/**Subclasses are parameterized by their own type*/
	private static abstract class SelfType<T extends SelfType<T>>{
		public abstract T getThis();
	}
 
	/**Supertype inherits directly from the parameterized SelfType*/
	private static class SuperType extends SelfType<SuperType>{
		@Override
		public SuperType getThis(){
			return this;
		}
	}
 
	/**Subtype inherits indirectly from the parameterized SelfType*/
	private static class SubType extends SuperType{}
 
	/**Creates a list containing a single SelfType*/
	public static <T extends SelfType<T>> List<T> makeSingletonList(T t){
		return Collections.singletonList(t);
	}
 
	/**
	 * Creates a list containing a single SelfType, allowing the list's
	 * element-type to be a supertype of the type of its single element
	 */
	public static <T extends SelfType<T>,S extends T> List<T> makeSingletonList2(S s){
		return Collections.singletonList((T)s);
	}
 
	public static void main(String[] args){
		/*making lists of super types works fine ...*/
		makeSingletonList(new SuperType());
		List<SuperType> lsup = makeSingletonList(new SuperType());
 
		/*but we can't make a list of sub types; seems weird ...*/
		List<SubType> lsub = makeSingletonList(new SubType()); //ERROR
		/*can't even call it w/o assigning the return value:*/
		makeSingletonList(new SubType()); //ERROR
 
 
		/*so instead, we should be able to make lists of super type containing sub type elements*/
		makeSingletonList2(new SubType()); //ERROR
		/*even if we assign the return value:*/
		lsup = makeSingletonList2(new SubType()); // ERROR (eclipse is okay with this though)
		/*this still doesn't work either:*/
		lsub = makeSingletonList2(new SubType()); // ERROR
 
		/*we can make lists of super type this way though*/
		makeSingletonList2(new SuperType()); // (eclipse doesn't like this though)
		/*also ok if we assign the return value*/
		lsup = makeSingletonList2(new SuperType());
	}
}
 
brucechapman
Posts:683
Registered: 11/24/98
Re: Generic methods and SelfTypes   
May 31, 2005 4:09 PM (reply 1 of 4)  (In reply to original post )

 
Anything you can do with makeSingletonList2() you can also do with makeSingletonList(), because makeSingletonList() can accept a subclass of its declared argument type (otherwise we wouldn't have polymorphism would we).

makeSingletonList2() attempts to make this more explicit, but I suspect only has the effect of making the method type argument inference algorithm work harder. I have a hunch (could easily be wrong), that in some cases it might need to use the assignment conversion rule to infer T which means it can't infere T if there is no assignment conversion on the result of the method call.

/*but we can't make a list of sub types; seems weird ...*/
List<SubType> lsub = makeSingletonList(new SubType()); //ERROR


The type of "new SubType()" expression which matches the method's signature is "SelfType<SuperType>" ( from SubType extends SuperType and SuperType extends SelfType<SuperType>) so T is SuperType, so the return type is List<SuperType>.

But that doesn't explain your error message, or the fact that the unassigned one fails also.

Is this one of those situations where a certain Peter says "mea culpa", or am I not spotting something here?

If you are keen, you could work the type inference algorithm by hand on this case.

download/view Java language Spec (3rd ed) here http://java.sun.com/docs/books/jls/index.html


section 15.12.2.7 (after checking that the preceding 15.12.2 steps select your method)
 
PeterAhe
Posts:408
Registered: 12/7/00
Re: Generic methods and SelfTypes   
May 31, 2005 5:46 PM (reply 2 of 4)  (In reply to #1 )

 
Is this one of those situations where a certain Peter
says "mea culpa"

That is to be determined. I think something is wrong and
have submitted bug 6278587. I'll evaluate it later this week.

Thanks for reporting this issue.
 
mmchenry@carnegielearning.com
Posts:3
Registered: 5/31/05
Re: Generic methods and SelfTypes   
Jun 1, 2005 6:48 AM (reply 3 of 4)  (In reply to #1 )

 
Anything you can do with makeSingletonList2() you can
also do with makeSingletonList(), because
makeSingletonList() can accept a subclass of its
declared argument type (otherwise we wouldn't have
polymorphism would we).

I agree that this should be true, but looking at the code sample from my original post, we have the following, which seems to violate polymorphism:

makeSingletonList(new SuperType()); //OK
makeSingletonList(new SubType()); //ERROR
 
mmchenry@carnegielearning.com
Posts:3
Registered: 5/31/05
Re: Generic methods and SelfTypes   
Jun 10, 2005 9:49 AM (reply 4 of 4)  (In reply to #3 )

 
This bug also posted to the eclipse project:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=98538
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 54
  • Guests : 123

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums