Expanded target typing
Dan Smith
daniel.smith at oracle.com
Thu Jul 19 11:23:51 PDT 2012
On Jul 10, 2012, at 5:29 AM, Deepak S Patwardhan wrote:
> Hello,
>
> Section 5, Contexts for target typing, in the State of the Lambda, 4th
> edition, ends with :
>
> -- begin quote
> The expanded role of target typing in the compiler is not limited to lambda
> expressions: generic method invocations and "diamond" constructor
> invocations can also take advantage of target types wherever they are
> available. The following declarations are illegal in Java SE 7 but valid
> under JSR 335:
>
> List<String> ls = Collections.checkedList(new ArrayList<>(), String.class);
> Set<Integer> si = flag ? Collections.singleton(23) : Collections.emptySet();
> -- end quote
>
> These don't compile with lambda build 39.
>
> Question 1) So, just wanted to know if this will be implemented or is no
> longer in scope for Java 8?
> Question 2) Assuming this will be implemented, will the following work ?
> (not allowed in Java SE 7)
>
> Map<String, List<String>> teamMembers = .
> teamMembers.put("team1", new ArrayList<>());
>
> I ask this because the two examples above seem to indicate that this may not
> work, but the following might:
>
> List<String> dontCare = teamMembers.put("team1", new ArrayList<>());
'teamMembers.put' is simpler than the two examples from State of the Lambda -- the signature of 'put' doesn't depend on inference. So this is a good example of a less-ambitious improvement in inference that is a side-effect of improving target typing for lambdas. It should definitely compile in 8.
(Testing, I see that it works with the latest lambda build, but didn't work with an older copy I have from May.)
What Brian said for the original two examples: those are the kinds of things we'd like to be able to handle, but we're still working on solutions.
—Dan
More information about the lambda-dev
mailing list