Captured wildcards
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Mar 19 12:28:51 UTC 2015
Thinking about it more, I'd say that using the upper bound of the
_wildcard_ associated to the captured type (as per Jan's patch) is
always correct; after all, that's the most likely explicit type the user
would come up with if storing the contents of the expression into some
variable. Some static type info is lost as a result, but that's not
terrible, assuming the only way to declare a variable in kulla is to
have an explicit variable declaration with a type. Of course if you
could do stuff like:
var x = foo.get()
you'll get into trouble if the inferred type for 'x' is less precise
than the type of foo.get().
Maurizio
On 19/03/15 12:22, Maurizio Cimadamore wrote:
> Upper bound should always be used - although I'm not 100% sure of what
> should happen when an upper bound is an intersection type:
>
> interface A { }
> interface B { }
>
> class E implements A, B { }
>
> class Foo<X extends A & B> {
> X x;
>
> Foo(X x) {
> this.x = x;
> }
>
> X get() {
> return x;
> }
> }
>
>
> Foo<?> foo = new Foo<>(new E());
> foo.get(); //???
>
> Maurizio
>
> On 19/03/15 11:28, Jan Lahoda wrote:
>> Hello,
>>
>> Assume this declaration:
>> List<? extends String> list = Arrays.asList("a");
>>
>> This expression fails:
>> -> list.iterator().next()
>> | Error:
>> | illegal start of type
>> | list.iterator().next()
>> | ^
>>
>> The reason is that "? extends String" is used as the type of the
>> expression, instead of "String":
>> ---
>> package REPL;
>> import java.util.regex.*;import java.util.*;import
>> java.util.concurrent.*;import java.util.prefs.*;import
>> java.net.*;import java.io.*;import java.math.*;import static
>> REPL.$REPL8.printf;
>> import static REPL.$REPL9.list;
>> public class $REPL11 {
>> public static
>> ? extends String $2;
>> public static Object do_it$() throws Throwable {
>> return $2 =
>> list.iterator().next();
>> }
>> }
>> ---
>>
>> Attached is a possible fix - what do you think?
>>
>> Thanks,
>> Jan
>
More information about the kulla-dev
mailing list