Consider the UnaryFunction
interface defined in Effective Java generics chapter .
public interface UnaryFunction<T> {
T apply(T arg);
}
and the following code for returning the UnaryFunction
// Generic singleton factory pattern
private static UnaryFunction<Object> IDENTITY_FUNCTION = new UnaryFunction<Object>() {
public Object apply(Object arg) { return arg; }
};
// IDENTITY_FUNCTION is stateless and its type parameter is
// unbounded so it's safe to share one instance across all types.
@SuppressWarnings("unchecked")
public static <T> UnaryFunction<T> identityFunction() {
return (UnaryFunction<T>) IDENTITY_FUNCTION;
}
Why is the cast of IDENTITY_FUNCTION
to (UnaryFunction<T>)
safe ?
The book says this about the question I am asking but I can't follow the logic here . Where are we invoking the apply
function which does the identity operation ? i am confused because it is that function which returns the same object passed into it without modifying anything .
The cast of IDENTITY_FUNCTION to
(UnaryFunction<T>)
generates an unchecked cast warning, asUnaryFunction<Object>
is not aUnaryFunction<T>
for everyT
. But the identity function is special: it returns its argument unmodified, so we know that it is typesafe to use it as aUnaryFunction<T>
whatever the value ofT
. Therefore, we can confidently suppress the unchecked cast warning that is generated by this cast. Once we’ve done this, the code compiles without error or warning.
With type erasure
actually is
Now for identity
one may assume that it always is correct (is equivalent to
y = x;
).(Very pragmatic.)
identityFunction() simply returns the function itself, to be used on different objects.
Following is a usage example:
The type safety warning is important. It's there because IDENTITY_FUNCTION is implemented such that the compiler can't guarantee that the function returns the same type as the input. Consider the following alternative implementation:
This implementation always returns a string, so it's obviously not safe to return it as a unary function on a generic data type.
In our case (IDENTITY_FUNCTION), the proof that the returned type is the same as the input type is inside the implementation. We return the same instance, so it's guaranteed to have the same type. When you suppress type safety warnings, it's recommended to justify it with a proof.
The cast is safe insomuch only as the identity function returns the exact object that was passed to it in the first place. As such, at runtime, there is no specialization of the generic parameter
T
that can violate the cast.Aka, you are casting an object as it's own type.