A DESCRIPTION OF THE REQUEST :
Although generics have reduced the need for casting there are still occasions where you need to do it. A cast method that uses type argument inference would make casting much easier.
So if this method existed in Object :
@SuppressWarnings("unchecked")
public <T> T cast()
{
return (T)this;
}
then most casts would be simplified to
// very contrived example
Object o = new MyType();
MyType myObject = o.cast();
instead of the current
MyType myObject = (MyType)o;
JUSTIFICATION :
This means less code to type and read. That has to be good!
Although generics have reduced the need for casting there are still occasions where you need to do it. A cast method that uses type argument inference would make casting much easier.
So if this method existed in Object :
@SuppressWarnings("unchecked")
public <T> T cast()
{
return (T)this;
}
then most casts would be simplified to
// very contrived example
Object o = new MyType();
MyType myObject = o.cast();
instead of the current
MyType myObject = (MyType)o;
JUSTIFICATION :
This means less code to type and read. That has to be good!
- relates to
-
JDK-4881275 (reflect) Class.cast() - typesafe cast desired
-
- Resolved
-