S
Sideswipe
So, a colleague of mine recently is trying to use generics to enforce
type-correctness (good) in an interface method that takes object. He
needs a specific subtype and so we try this:
public void someMethod(Object myType)
if(SomeGeneric instanceof MyType) ... // compile error
if(SomeGeneric.class.isAssignableFrom(MyType.class)) // compile error
SomeGeneric var = null;
try {
var = (SomeGeneric)myType; // compiles, and works as expected.
}
catch(ClassCastException cce) {
// invalid type, ignore
}
If Generics are a type substitution and erase, no matter WHAT type
'SomeGeneric' is from is MUST has '.class' and work with 'instanceof'
I don't get why those fail. Can someone explain?
type-correctness (good) in an interface method that takes object. He
needs a specific subtype and so we try this:
public void someMethod(Object myType)
if(SomeGeneric instanceof MyType) ... // compile error
if(SomeGeneric.class.isAssignableFrom(MyType.class)) // compile error
SomeGeneric var = null;
try {
var = (SomeGeneric)myType; // compiles, and works as expected.
}
catch(ClassCastException cce) {
// invalid type, ignore
}
If Generics are a type substitution and erase, no matter WHAT type
'SomeGeneric' is from is MUST has '.class' and work with 'instanceof'
I don't get why those fail. Can someone explain?