Proposal: Improved Wildcard Syntax for Java
Neal Gafter
neal at gafter.com
Sat Feb 28 09:03:09 PST 2009
Improved Wildcard Syntax for Java
http://docs.google.com/Doc?id=ddb3zt39_79g2mtpccz
AUTHOR(S):
Neal Gafter
OVERVIEW
The current syntax for Java's wildcards is confusing enough that
mneumonics are required to read code. We suggest adding an
alternative syntax that is much more readable, and then migrating code
to the more readable syntax over time.
FEATURE SUMMARY:
[Note: since this is an additional syntax for a feature already in the
language, most existing tutorial material can be used.]
A covariant wildcard generic argument can be written either "? extends
Y" or "out Y".
A contravariant wildcard generic argument can be written either "?
super Y" or "in Y".
MAJOR ADVANTAGE:
While the existing syntax is mneumonic from the point of view of
describing its effect on the type system, it is not mneumonic in its
use. The new syntax is self-mneumonic for users.
MAJOR BENEFIT:
Jave code that uses wildcards is more readable, as are diagnostics
involving them.
MAJOR DISADVANTAGE:
Two ways of doing the same thing, though we hope code will migrate
over time toward the new more readable syntax.
ALTERNATIVES:
None.
EXAMPLES
SIMPLE EXAMPLE:
interface Collection<E> {
boolean addAll(Collection<out E> c);
...
}
ADVANCED EXAMPLE:
class Collections {
public static <T extends Comparable<in T>> void sort(List<T> list) {...}
public static <T> void sort(List<T> list, Comparator<in T> c) {...}
int binarySearch(List<out Comparable<in T>> list, T key) {...}
public static <T> void fill(List<in T> list, T obj) {...}
public static <T> void copy(List<in T> dest, List<out T> src) {
...
}
DETAILS
SPECIFICATION:
"in" and "out" are added as context-sensitive keywords with precisely
the same meaning of "? super" and "? extends". As context-sensitive
keywords, they continue to be usable as identifiers in all contexts.
COMPILATION:
As today.
TESTING:
As today, though a bit of additional work to ensure that the context
sensitive keyword is handled properly by the compiler. Specifically,
that they continue to be fully usable as identifiers in other
contexts.
LIBRARY SUPPORT:
None.
REFLECTIVE APIS:
None.
OTHER CHANGES:
None.
MIGRATION:
A trivial textual substitution can be used to translate old code to
the new syntax.
COMPATIBILITY
BREAKING CHANGES:
None.
EXISTING PROGRAMS:
No impact.
REFERENCES
EXISTING BUGS:
None
URL FOR PROTOTYPE (optional):
None
More information about the coin-dev
mailing list