Strange Generics Behavior

B

Benjamin Lerman

Hi all,

I encounter a strange behavior with generics that I do not understand.

I have the following interfaces:

public interface Int1<E> {
}

and

public interface Int2<E> extends Int1<Integer> {
}

and I have somewhere the following lines:

void foo(Int2 o) {
Int1<Integer> p = o;
}

The java compiler gives the following warning:

Type safety: The expression of type Int2 needs unchecked conversion to
conform to Int1<Integer>

I do not understand why this warning exists. Moreover it prevents the
compiler to understand that an Int2<E> is also an Int1<Integer>, so it
prevents a lot of type checking.

Is there any solution ?

Thanks.

Benjamin Lerman
 
B

Benjamin Lerman

There can't be such.
It should be:

interface Int2 extends Int1<Integer> {

}

Why?

Note that E in Int2 is not Integer. I want to have something like:

public class O1 implements Int2<Float> {
}

Benjamin
 
C

Chris Uppal

Benjamin said:
Type safety: The expression of type Int2 needs unchecked conversion to
conform to Int1<Integer>

I do not understand why this warning exists. Moreover it prevents the
compiler to understand that an Int2<E> is also an Int1<Integer>, so it
prevents a lot of type checking.

I think the warning is spurious. I suspect that there's a bug somewhere.

The compiler (as far as I know) emits this warning when it is unable to prove
to itself that an operation is safe /and/ cannot emit a runtime checkcast
operation (because the types are identical after erasure). In this case there
is no checkcast (correctly), but I can't see why it shouldn't realise that
anything which conforms to Int2<anything> necessarily also conforms to
Int1 said:
Is there any solution ?

Just ignore it.

-- chris
 
S

Simon

Hi,
public interface Int1<E> {
}

and

public interface Int2<E> extends Int1<Integer> {
}

and I have somewhere the following lines:

void foo(Int2 o) {
Int1<Integer> p = o;
}

The java compiler gives the following warning:

Type safety: The expression of type Int2 needs unchecked conversion to
conform to Int1<Integer>

I'm not sure why you get that warning, but you can avoid it if you declare foo
like this:

void foo(Int2<Object> o)

You can reproduce the same warning with the following simpler code that doesn't
use inheritance:

Int1 o = null;
Int1<Object> p = o;

I wonder what the difference is between Int1 and Int1<Object>. Is there any
example where this is significant or rather where using Int1 instead of
Int1<Object> causes any problems/exceptions?
 
P

Piotr Kobzda

Benjamin said:
Is there any solution ?

This:

void foo(Int2 o) {
Int1<Integer> p = (Int2<?>) o;
}

or better:

void foo(Int2<?> o) {
Int1<Integer> p = o;
}


Java compilers treat raw types specially. :)


Regards,
piotr
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Simon schreef:

Indeed, for the same reason why you get this warning when you use List
I'm not sure why you get that warning, but you can avoid it if you declare foo
like this:

void foo(Int2<Object> o)

Well, I would declare foo like something more meaningful than
but that entirely depends on what you meant the second said:
You can reproduce the same warning with the following simpler code that doesn't
use inheritance:

Int1 o = null;
Int1<Object> p = o;

I wonder what the difference is between Int1 and Int1<Object>. Is there any
example where this is significant or rather where using Int1 instead of
Int1<Object> causes any problems/exceptions?

There is a huge difference between Collection<Object> and just
Collection. For example Collection<Integer> is an heir of Collection,
but not of Collection<Object>. (Interestingly, in Eiffel this is not
the case. I wonder how they handle the problems that arise out of this.
They had generics from the start, so the whole type erasure stuff does
not exist.)

H.
- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFESNSYe+7xMGD3itQRAtDpAJ9Qqax29LjdvKA66Y3JMAEzAfes5QCdH8jb
PKcdw99zSEfFbvCfXxu6oBU=
=pdY/
-----END PGP SIGNATURE-----
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

generics puzzle 57
Generics ? 14
Reflection in 1.5 1
Generics 12
inconsistent compiler behavior with generics example 3
Generics annoyance 18
can this be done with generics? 32
Generics Issue 13

Members online

Forum statistics

Threads
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top