Created
July 19, 2010 20:22
-
-
Save gamlerhart/481935 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Even a cast generates a warning | |
List<MyClass> data = (List<MyClass>) legacyLibraryCall(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static <TTarget,TSource> TTarget cast(TSource source){ | |
return (TTarget)source; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generates an unchecked warning | |
List<MyClass> data = legacyLibraryCall(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static <T> List<T> newList(){ | |
return new ArrayList<T>(); | |
} | |
public static void main(String[] args) { | |
// Java infers the type T for the generic method newList | |
List<MyClass> data = newList(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object result = anOperation(); | |
MyClass data = (MyClass) result; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Now we can use the cast-method. Note that this won't generate a warning | |
// because the return-type is inferred. | |
List<MyClass> data = cast(legacyLibraryCall()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment