FileDocCategorySizeDatePackage
InferenceDemo.javaAPI DocExample4549Mon Nov 24 23:38:34 GMT 2003oreilly.hcj.tiger

InferenceDemo

public class InferenceDemo extends Object
Implements a manager of lists that stores the lists by key.

Fields Summary
static int
callCount
Constructors Summary
Methods Summary
public static final voidmain(java.lang.String[] args)
Demonstrate the rules.

		System.out.println();

		// Picks Sixth
		System.out.print("\n1) ");
		someMethod("Hello");

		// Ambiguous, wont compile.
		//System.out.print("\n2) ");
		//someMethod(new C());

		// Ambiguous, wont compile.
		// First and Fifth mask each other and can never be called.
		//System.out.print("\n3) ");
		//someMethod(new Object());

		// Picks Second
		System.out.print("\n5) ");
		someMethod(new Integer(5));

		// Picks Second
		System.out.print("\n6) ");
		someMethod(5);

		// Picks Fourth
		System.out.print("\n7) ");
		List<Integer> listOne = someMethod();
		listOne.add(new Integer(5));

		System.out.print("\n7a) ");
		List<String> listTwo = someMethod();
		listTwo.add(new String("Hello"));

		System.out.print("\n7b) ");
		Object listThree = someMethod();
		System.out.println(listThree.getClass());
		//listThree.add( ... );  // Cant put anything into this list

		// -- Make some lists --
		List<A> listOfA = new ArrayList<A>();
		listOfA.add(new A());

		List<B> listOfB = new ArrayList<B>();
		listOfB.add(new B());
		
		// Picks 
		System.out.print("\n8) ");
		someMethod(5, new ArrayList<Integer>());

		// Wont compile: No such type. Since A can not necessarily be promoted to B.
		//System.out.print("9) ");
		//someMethod(new A(), listOfB);

		// Picks Three: Works because B can be demoted to A
		System.out.print("\n10) ");
		someMethod(new B(), listOfA);

		// Picks Seventh
		System.out.print("\n11) ");
		someOtherMethod(new A(), listOfB);

		// Picks Seventh
		// Works even though it probably shouldnt. The someOtherMethod() signature implies
		// that the type of the component of the ist should be a subtype of the type used
		// for obj. However, in this case first is a List<A> and A clearly isnt a subtype
		// of B. Is this a bug?
		System.out.print("\n12) ");
		someOtherMethod(new B(), listOfA);

		// Really weird as the two types arent even remotely related. This should probably
		// be rejected by the compiler but it doesnt seem to be enforcing its bounds. 
		System.out.print("\n13) ");
		List<String> listOfString = new ArrayList<String>();
		listOfString.add("Hello");
		someOtherMethod(new Integer(5), listOfString);

		// Doesnt work even though the spec says it should: 
		//System.out.print(I"14)");
		//A value = <A>someMethod(new B(), new ArrayList<A>());

		// Picks Fourth: The type of the list returned by SomeMethod is List<Integer> 
		System.out.print("\n15) ");
		someMethod(5, someMethod());

		// Picks Fourth: The type of the list returned by SomeMethod is List<String> 
		System.out.print("\n16) ");
		someMethod("Hello", someMethod());
	
public static TypesomeMethod(Type obj)
First.


	  	
	       
		System.out.print("First==> ");
		return obj;
	
public static TypesomeMethod(Type num)
Second.

		System.out.print("Second==> ");
		return num;
	
public static TypesomeMethod(Type obj, java.util.List list)
Third.

		System.out.print("Third==> ");
		return obj;
	
public static java.util.ListsomeMethod()
Fourth.

		System.out.print("Fourth==> ");
		return new ArrayList<Type>();
	
public static voidsomeMethod(java.lang.Object obj)
Fifth.

		System.out.print("Fifth==> ");
	
public static voidsomeMethod(java.lang.String str)
Sixth.

		System.out.print("Sixth==> ");
	
public static TypesomeOtherMethod(Type obj, java.util.List list)
Seventh.

		System.out.print("Seventh==> ");
		return obj;