Proposal: Improved Wildcard Syntax for Java (original) (raw)

Joseph D. Darcy Joe.Darcy at Sun.COM
Tue Mar 17 13:36:40 PDT 2009


Hello.

While the existing "? extends" and "? super" syntax is certainly a bit obtuse (but better than "+" and "-"!), I don't find introducing redundant ways of declaring those semantics attractive at this point.

-Joe

Neal Gafter wrote:

Improved Wildcard Syntax for Java http://docs.google.com/Doc?id=ddb3zt3979g2mtpccz

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 { boolean addAll(Collection c); ... } ADVANCED EXAMPLE: class Collections { public static <T extends Comparable> void sort(List list) {...} public static void sort(List list, Comparator c) {...} int binarySearch(List<out Comparable> list, T key) {...} public static void fill(List list, T obj) {...} public static void copy(List dest, List 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