Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6840638
Votes 0
Synopsis Project Coin: Improved Type Inference for Generic Instance Creation (aka 'diamond')
Category java:compiler
Reported Against
Release Fixed 7(b72)
State 10-Fix Delivered, request for enhancement
Priority: 3-Medium
Related Bugs 6242254 , 6368076 , 4879776 , 6878724 , 6880112 , 4983159 , 5092426 , 6220689
Submit Date 13-MAY-2009
Description
One of the language changes in Project Coin is allowing inference on constructor call through the <> notation (aka 'diamond'); this bug tracks that work.

SUMMARY OF THE PROPOSAL

This proposal addresses the addition of limited type inference for
class instance creation expressions to the Java programming language.
In cases where parametrized types need to be explicitly declared for a
constructor, and the full parametrized type < T1 , T2 , ...Tn > of
that constructor is obvious from the context, then the  parameterized
type of the constructor can be replaced with an empty set of type
parameters: <>. The <>  construct is legal to use when constructing an
ob ject and either assigning it to a variable, or when passing  it as
a parameter.

For example, consider the following assignment statement:

Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

This is rather lengthy, so it can be replaced with this:

Map<String, List<String>> anagrams = new HashMap<>();
Posted Date : 2009-05-13 15:38:59.0
Work Around
N/A
Evaluation
This feature will significantly reduce the code that the user should type for initializing generic classes.

Let's analyze what is the expected behavior of a prototype supporting this feature. Given the following (generic) class declaration:

class Foo<X> {
...
Foo(X x) {...}
...
}

the '<>' notation should allow to initialize a generic class without explicitly providing an instantiation for Foo's type parameters (X in this case). Instead, the type parameters are inferred from the surrounding assignment context (if available).

e.g.

Foo<String> foo = new Foo<>(); //inferred as Foo<String>

A possible Java-model for implementing '<>' is to translate the constructor call into a call to a static synthetic factory method that the compiler adds to Foo.

class Foo<X> {
...
Foo(X x) {...}
static <Z> Foo<Z> create$(Z z) {}
...
}

Foo<String> foo = new Foo<>(); --> Foo<String> = Foo.create$();

Note: the purpose of the above translation is to give an idea about the semantics of the diamond operator and not about the actual implementation. An actual implementation should take into account aspects as:

* Generic constructors that declare their own type variables
* accessibility modifiers
* var-args
* boxing/unboxing conversion
Posted Date : 2009-09-23 10:06:00.0
Comments
  
  Include a link with my name & email   

Submitted On 22-SEP-2009
Tashtego2008
Foo<String> = new Foo<>(); --> Foo<String> = Foo.create$();

? doesnt this have to be

Foo<String> foo = new Foo<>(); --> Foo<String> foo = Foo.create$();



PLEASE NOTE: JDK6 is formerly known as Project Mustang