Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created April 21, 2011 12:26
Show Gist options
  • Select an option

  • Save lukaspili/934374 to your computer and use it in GitHub Desktop.

Select an option

Save lukaspili/934374 to your computer and use it in GitHub Desktop.
Generics implicit cast ?
import java.util.List;
import java.util.ArrayList;
public class Foo {
public <T> List<T> getGenerics() {
return new ArrayList<T>();
}
public <T> T getFoo() {
return getGenerics().get(0); // Compilation error
}
public <T> T getBar() {
List<T> bar = getGenerics();
return bar.get(0); // OK
}
// AND THIS WORKS :
public <T> T getGeneric() {
return null; // we don't care, the problem happens at compilation time
}
public <T> T getFoobar() {
return getGeneric(); // OK
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment