Use of JCP site is subject to the
JCP Terms of Use and the
Oracle Privacy Policy
|
JSRs: Java Specification Requests
JSR 65: Concise Object-Array Literals
This JSR has been Withdrawn The change of Specification Lead has resulted in the following change to the original JSR.
Section 1. Identification Submitting Participant: Sun Microsystems, Inc. Name of Contact Person: Gilad Bracha E-Mail Address: gilad.bracha@eng.sun.com Telephone Number: 408-863-3116 Fax Number: 408-343-1797 Original Java Specification Request (JSR) Identification | Request | Contributions Section 1. Identification Submitting Participant: Sun Microsystems, Inc. Name of Contact Person: Mark Reinhold E-Mail Address: mr@eng.sun.com Telephone Number: 408-343-1830 Fax Number: 408-343-1797
Section 2: Request
The Java programming language provides no convenient way to pass a
variable number of arguments to a method, similar to the varargs
feature of C or the &rest keyword of Common Lisp. This
feature is extremely useful in GUI APIs, in constructing and
manipulating collections, and in APIs for formatted output. One of the goals of
JSR-51, New
I/O APIs for the Java Platform, for example, is to provide a
simple API for formatted textual output similar to the well-known
printf procedure of the standard C library. The central
method of such an API accepts a format string followed by some
arguments, where the exact number of arguments required depends upon
the content of the format string. Ideally we'd like to be able to
write something like this:
2.2 What is the target Java platform? (i.e., desktop, server, personal, embedded, card, etc.)All platforms that make use of the full Java programming language: Personal, embedded, desktop, and server. 2.3 What need of the Java community will be addressed by the proposed specification?The proposed specification will increase developer productivity by providing direct language support for a very common programming idiom. It will make Java that much more attractive an alternative to other competing platforms. 2.4 Why isn't this need met by existing specifications?There are three ways to support varying numbers of arguments without changing the language. No one is completely satisfactory. Massive overloading Declare many
different methods, all with the same name but different signatures, reflecting
all the possible combinations of argument counts and types. In the formatting
API, for example, declare
This technique can work if there are only a few possible types and if there is a fairly small lower bound on the total number of arguments. Otherwise the number of methods required tends to explode combinatorically. For the formatting API there are nine argument types and no limit to the number of arguments. Even a fixed limit, say six, would require 531,441 distinct methods. Object arrays Declare a single
formatting method that takes a format string and an array of objects, requiring
the invoking code to package its arguments into an object array itself:
Chained methods Declare a single
formatting method that takes a format string and returns an auxiliary
argument collector object that collects and processes the arguments:
A class implementing the ArgumentCollector interface has one method for each
possible argument type as well as an end method to identify the end of
the argument list. The argument methods return the ArgumentCollector object
itself, allowing arguments to be passed with a fairly compact syntax:
No one of the above alternatives is completely satisfactory. The first is impractical in general, the second is extremely verbose, and the third is rather odd in appearance and somewhat error prone. These choices suggest that a very small, tightly focussed change to the Java programming language might be a better solution. 2.5 Please give a short description of the underlying technology or technologies:The best approach to solving the stated problem appears to be to add a facility for concise, automatically-boxing object-array literals. In short, the syntax of expressions would be extended to allow a list of expressions within curly braces; for example, int i; double x; String s; ... { i, x, s } ...This would be expanded by the compiler into an object-array literal that "boxes" any values of primitive type using the appropriate wrapper classes from the java.lang package: ... new Object[] { new Integer(i), new Double(x), s } ...Thus the above formatting example could be written very concisely as: f.fmt("%d bytes in %d seconds (%.2f KB/s)\n", { nbytes, seconds, ((double)(nbytes / 1024) / (double)seconds) });This proposal has the following advantages:
2.6 Is there a proposed package name for the API Specification? (i.e., javapi.something, org.something, etc.)Not applicable: This is a language change only. 2.7 Does the proposed specification have any dependencies on specific operating systems, CPUs, or I/O devices that you know of?No. 2.8 Are there any security issues that cannot be addressed by the current security model?No. 2.9 Are there any internationalization or localization issues?No. 2.10 Are there any existing specifications that might be rendered obsolete, deprecated, or in need of revision as a result of this work?The Java Language Specification will require minor revision as a result of this work. Existing specifications that make use of object-array arguments could, at the option of their maintainers, be revised to suggest that the new concise object-array literal notation be employed. Section 3: Contributions
3.1 Please list any existing documents, specifications, or implementations that describe the technology. Please include links to the documents if they are publicly available.Sun has developed a preliminary set of changes to the Java Language Specification that describe the syntax and semantics of concise object-array literals. This document is not publicly available. 3.2 Explanation of how these items might be used as a starting point for the work.The draft JLS changes will serve as a starting point for the work of the Expert Group. |