Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Method Infer Type Parameter

 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following method is declared in class Utilities:

static <E, K extends E> boolean containsV3(K key, E[] array) {
for(E element : array)
if(key.equals(element)) return true;
return false;
}

Why do these calls result in errors?:

assert Utilities.containsV3("Hi", intArray) == false; // (10) Compile-time error!
assert Utilities.containsV3(30.5, intArray) == false; // (11) Compile-time error!

Can't E be inferred to be Object in 1st call and Number in 2nd call?
 
Ankit nShah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jeet,

static <E, K extends E> boolean containsV3(K key, E[] array) {

declaration means that containsV3 accepts two arguments one is array of E type and other is K which should be sub type of E.

In your first call
assert Utilities.containsV3("Hi", intArray) == false; // (10) Compile-time error!
E seems to be int[] and K is String so K subtype of E does not hold true and hence the error

Similarily for second
assert Utilities.containsV3(30.5, intArray) == false; // (11) Compile-time error!
E seems to be int[] and K is autoboxed to Double so K is also not subtype of E

One working eg for this could be
Number[] d = {1.2 ,3.4};
assert Cloning.containsV3(1.2, d) == true;
Here E is Number and K is Double subtype of Number.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply sorry i should have mentioned it that intArray is Integer[]

But i dont get why cant E be inferred to be Object so then String is a subtype of Object and Integer[] is a subtype of Object[] in the 1st call and the call succeeds? Similarly why cant E be inferred to be Number in the 2nd call so that Double is subtype of Number and Integer[] is subtype of Number[] and this call too succeeds?
 
Ankit nShah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Since you are passing Integer[], E is taken as Integer. and K should be a subtype of Integer only.

But i dont get why cant E be inferred to be Object

If this would happen then since all object inherits from Object, E will always be Object and there is no meaning in providing types.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then even this should give an error as E can always be Object:

static <E> boolean containsV2(E key, E[] array) { // (2) Generic version
for(E element : array)
if(key.equals(element)) return true;
return false;
}


assert Utilities.containsV2("Hi", intArray) == false;
 
Ankit nShah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about not framing it properly.
What I meant was that if E was inferred as Object, then every other object would be a subtype of E and so your K which is subtype of E wont have any meaning.
 
Jeet Jain
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhh ok. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic