[lvti] Non-denotable types

Tagir Valeev amaembo at gmail.com
Fri Jul 14 05:15:48 UTC 2017


Hello!

When thinking about local variable type inference, I noticed that lambda
parameter type inference in Java 8 is sometimes similar. For example, we
can declare the following method:

    static <T> void var(T t, Consumer<? super T> cons) {
        cons.accept(t);
    }

Then Java 8 code like this:

   var(theValue, theVariable -> {
      codeBlock;
   });

Is equivalent to LVTI code like this:

  var theVariable = theValue;
  codeBlock;

Apparently this does not work for non-denotable types. I checked the
non-denotable types section in [1]: seems that lambda parameter inference
infers to non-denotable type as it described in this section, which already
produces "surprising" results:

    void test(List<?> l1, List<?> l2) {
        var(l1, l3 -> {
            l3 = l2; // error
            l3.add(l3.get(0)); // error
        });
    }

Shouldn't we expect a consistency here? There's already a kind of variables
in Java (lambda parameters) for which non-denotable types are inferred. It
looks strange to me that two kinds of variable inference work in different
manner.

With best regards,
Tagir Valeev

[1] http://openjdk.java.net/jeps/286


More information about the amber-dev mailing list