Question

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, as UnaryFunction<Object> is not a UnaryFunction<T> for every T. But the identity function is special: it returns its argument unmodified, so we know that it is typesafe to use it as a UnaryFunction<T> whatever the value of T. 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.

Was it helpful?

Solution

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.

OTHER TIPS

With type erasure

T apply(T arg);

actually is

Object apply(Object arg);

Now for identity

Abc x = ...;
Abc y = IDENTITY.apply(x);

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:

String result = identityFunction().apply("Hello");

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:

private static UnaryFunction<Object> CONST_FUNCTION = new UnaryFunction<Object>() {
    public Object apply(Object arg) { return "Default"; } 
};

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top