-
Bug
-
Resolution: Won't Fix
-
P4
-
21
We want to avoid casting to a subtype of Element when querying VisibleMemberTable for a specific kind of element. Such casting is inconvenient, error-prone, and not DRY.
For example, this method could return `List<ExecutableElement>` if the kind is METHODS or CONSTRUCTORS, `List<VariableElement>` if the kind is FIELDS or ENUM_CONSTANTS, or `List<TypeElement>` if the kind is NESTED_CLASSES:
public List<Element> getVisibleMembers(Kind kind)
Unfortunately, we cannot parameterize Kind with a subtype of Element; generic enums are problematic, see: https://openjdk.org/jeps/301. So this is not an option:
public List<T extends Element> getVisibleMembers(Kind<T> kind)
However, we could use a type token (`Class<T extends Element>`), which would be more convenient, but still error-prone and not DRY:
public List<T extends Element> getVisibleMembers(Kind kind, Class<T> clazz)
Alternatively, we could emulate generic enums using sealed generic interfaces: interfaces can be generic while sealing and pattern matching for switch would allow enum-like treatment.
For example, this method could return `List<ExecutableElement>` if the kind is METHODS or CONSTRUCTORS, `List<VariableElement>` if the kind is FIELDS or ENUM_CONSTANTS, or `List<TypeElement>` if the kind is NESTED_CLASSES:
public List<Element> getVisibleMembers(Kind kind)
Unfortunately, we cannot parameterize Kind with a subtype of Element; generic enums are problematic, see: https://openjdk.org/jeps/301. So this is not an option:
public List<T extends Element> getVisibleMembers(Kind<T> kind)
However, we could use a type token (`Class<T extends Element>`), which would be more convenient, but still error-prone and not DRY:
public List<T extends Element> getVisibleMembers(Kind kind, Class<T> clazz)
Alternatively, we could emulate generic enums using sealed generic interfaces: interfaces can be generic while sealing and pattern matching for switch would allow enum-like treatment.