On 11/12/11 15:21, Rémi Forax wrote:
On 12/10/2011 12:38 PM, maurizio cimadamore wrote:
On 09-Dec-11 10:56 PM, Rémi Forax wrote:
Is there a reason why the diamond syntax can't be used
with an array ?
List<?>[] list = new List<>[12];
Because the current inference rules would end up inferring:
List<?>[] list = new List<Object>[12];
If you special-cased diamond on arrays so that it is inferred as:
List<?>[] list = new List<?>[12];
Then it would be safe.
but in that case
List<?>[] list = new List<?>[12];
is not equivalent to
static <E> List<E>[] foo() { ... }
...
List<?>[] list = foo();
because as you said, Object will be inferred.
Is there another case where diamond inference behave differently from
method inference ?
No - that's why the array case is banned; after going back and forth we
decided to adopt the principle of least surprise, which means diamond
always behave as a static generic method (the diamond spec is also
design to map the diamond case back to the generic factory method). What
I was saying is that in order for diamond to be safely used in array
context we'd need to revisit both the spec and the compiler, and to
rethink the diamond vs. generic method inference strategy. I think it
will be much more productive if we could remove the ban about creating
an array of List<Object> (reification) - which then would mean that the
current inference strategy will apply to arrays as well.
Maurizio
Maurizio
Rémi