thrown types cannot be inferred from lambda body because of cyclic inference
maurizio cimadamore
maurizio.cimadamore at oracle.com
Sun Oct 31 04:13:58 PDT 2010
On 30/10/2010 17:39, Thomas Jung wrote:
> Hi,
>
> the following does not compile:
>
> import com.google.common.base.*;
> class X{
> private Predicate<Integer> x = new Predicate<Integer>(){
> public boolean apply(Integer v) { return v % 3 == 0; }
> };
> Predicate<Integer> y = #{Integer v -> v % 3 == 0 };
> Predicate<Integer> z = #{v -> v % 3 == 0 }; //does not compile
> }
>
> X.java:11: warning: thrown types cannot be inferred from lambda body
> because of cyclic inference
> Predicate<Integer> z = #{v -> v % 3 == 0 }; //does not compile
> ^
> explicit type required for the following parameter: v
> 1 warning
>
> The error message is a bit strange. I can't see a cycle. Could someone
> explain it?
The message means that the body of the lambda expression cannot be
attributed before the target type (Predicate<Integer>) is known. In this
particular example, this has no effect; if you were calling a generic
method with some 'throws' type-variable there could have been problems
though. I think that in examples like this, where the target type is
provided in an assignment context, the compiler could do better job than
this.
Maurizio
> Thomas
>
More information about the lambda-dev
mailing list