Skip to content

Instantly share code, notes, and snippets.

@MLLeKander
Last active September 29, 2015 14:45
Show Gist options
  • Save MLLeKander/d4deaf9eeb3dfe9dd640 to your computer and use it in GitHub Desktop.
Save MLLeKander/d4deaf9eeb3dfe9dd640 to your computer and use it in GitHub Desktop.
$ javac Generics.java -Xlint:unchecked
Generics.java:17: error: method dblList in class Generics cannot be applied to given types;
dblList(lst);
^
required: List<EType>
found: List<CAP#1>
reason: inferred type does not conform to declared bound(s)
inferred: CAP#1
bound(s): Enum<CAP#1>
where EType is a type-variable:
EType extends Enum<EType> declared in method <EType>dblList(List<EType>)
where CAP#1 is a fresh type-variable:
CAP#1 extends Enum<?> from capture of ? extends Enum<?>
1 error
import java.util.*;
class Generics {
public enum A { A1, A2 }
public enum B { B1, B2 }
public static List<? extends Enum<?>> listFactory(String[] args) {
if (args.length == 0) {
return new ArrayList<>(Arrays.asList(A.A1, A.A2));
} else {
return new ArrayList<>(Arrays.asList(B.B1, B.B2));
}
}
public static void main(String[] args) {
List<? extends Enum<?>> lst = listFactory(args);
dblList(lst);
System.out.println(lst);
}
public static <EType extends Enum<EType>> void dblList(List<EType> lst) {
int size = lst.size();
for (int i = 0; i < size; i++) {
lst.add(lst.get(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment