<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi,<br>
as currently specified, the type of the expression used in array
access must be an array type - see JLS 15.10.3</p>
<blockquote type="cite">
<p class="norm-error">The type of the array reference expression
must be an array type (call it <span class="type">T</span><code class="literal">[]</code>, an array whose components are of
type <span class="type">T</span>), or a compile-time error
occurs. </p>
</blockquote>
<div class="moz-cite-prefix">There is nothing in the JLS to suggest
that an array type is anything other than some type T followed by
array brackets. E.g. in JLS 10.1:</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">
<blockquote type="cite">
<p class="norm-static">An array type is written as the name of
an element type followed by some number of empty pairs of
square brackets <code class="literal">[]</code>. The number
of bracket pairs indicates the depth of array nesting. </p>
</blockquote>
So, javac is behaving as specified in the JLS. Note that wildcards
are a distraction here. The wildcard generates a captured type -
that is a type variable whose upper bound is an array type.</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">The following program:</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">```<br>
class Foo<X extends Object[]> {<br>
void test(X x) { Object o = x[0]; } <br>
}<br>
</div>
<div class="moz-cite-prefix">```<br>
<br>
Exhibits the same behavior - e.g. it fails to compile, because "x"
is not an array.</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">So, the question you are asking is: why
can't we assume that a type-variable whose upper bound is an array
type is also an array type?</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">This is discussed here:</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix"><a class="moz-txt-link-freetext" href="https://bugs.openjdk.org/browse/JDK-8013843">https://bugs.openjdk.org/browse/JDK-8013843</a></div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">My reading is that it's doable, but
it's work to make sure that nothing else gets affected. E.g. if we
treat as array anything that has an array supertype, then the
"null type" might get tangled in the mix (e.g. null[0]). Of course
this is not a reason for not doing it - but some care is required.
For-each loops might be affected as well.</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">Cheers</div>
<div class="moz-cite-prefix">Maurizio<br>
</div>
<div class="moz-cite-prefix"><br>
</div>
<div class="moz-cite-prefix">On 13/01/2024 12:38, Attila Kelemen
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CAKDaPBc2ZnGWpuy2WTFZHe7H2ZHqr0+OUj=WAwtwMqWZuv2F7A@mail.gmail.com">
<div dir="ltr">Hi,
<div><br>
</div>
<div>I have not found anything in JLS specifically mentioning
this. So, I'm not sure if it is intentional, but the following
code does not compile (in any versions of Java I have tried):</div>
<div><br>
</div>
<div>```</div>
<div>interface ForEachable<T> {<br>
void forEach(Consumer<? super T> consumer);<br>
}<br>
<br>
List<String> wrongMethod(ForEachable<? extends
String[]> arrays) {<br>
var result = new ArrayList<String>();<br>
arrays.forEach(array -> result.add(array[0]));<br>
return result;<br>
}<br>
</div>
<div>```</div>
<div><br>
</div>
<div>or in an even simpler example:</div>
<div><br>
</div>
<div>```</div>
<div>String first(Supplier<? extends String[]> supplier) {<br>
return supplier.get()[0];<br>
}<br>
</div>
<div>```</div>
<div><br>
</div>
<div>The compiler complains about `array[0]` that `array` is not
an array. While I get that the compiler cannot assume that
there is no subtype of `String[]`, but even if there was one,
I would expect it to be an array, and be indexable.</div>
<div><br>
</div>
<div>An obvious workaround is to assign `array` to a new local
variable, but it is awkward to do.</div>
<div><br>
</div>
<div>Is this behavior a bug, or something required by the JLS?</div>
<div><br>
</div>
<div>Thanks,</div>
<div>Attila</div>
</div>
</blockquote>
</body>
</html>