On 06/12/11 08:01, Rémi Forax wrote:
On 12/06/2011 08:07 AM, David Holmes wrote:
On 6/12/2011 12:14 PM, David Holmes wrote:
On 6/12/2011 11:45 AM, Rémi Forax wrote:
On 12/06/2011 02:12 AM, David Holmes wrote:
Is the reason for constructs like this:
HashEntry<K,V>[] tab = (HashEntry<K,V>[])new HashEntry<?,?>[cap];
that we can't utilize diamond? Otherwise it would nicely reduce to:
HashEntry<K,V>[] tab = new HashEntry<>[cap];
This should not compile because otherwise you can write
This compiles fine:
@SuppressWarnings("unchecked")
public class TestGenerics<K,V> {
static class HashEntry<K,V> { }
public void m() {
HashEntry<K,V>[] newTable = new HashEntry<>[1];
}
}
I don't know exactly what the type inference code does but I assumed it
effectively converted the above to:
HashEntry<K,V>[] newTable = (HashEntry<K,V>[]) new HashEntry[1];
and not to:
HashEntry<K,V>[] newTable = new HashEntry<K,V>[1];
which of course does not compile.
Hmmm. Perhaps this is a compilation accident? I don't see anything in
the spec that allows diamond to be used with array creation expressions.
Yes, it's a bug of javac, eclipse compiler rejects this code.
Was a bug [1] ;-) - if you compile with a recent JDK 8 compiler, you get
the following:
TestBug.java:12: error: cannot create array with '<>'
HashEntry<K,V>[] newTable = new HashEntry<>[1];
1 error
[1] - http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7057297
Maurizio
David
-----
Rémi