Why is this type inference not working with this Lambda expression scenario?

后端 未结 6 761
小蘑菇
小蘑菇 2020-12-24 01:23

I have a weird scenario where type inference isn\'t working as I\'d expect when using a lambda expression. Here\'s an approximation of my real scenario:

stat         


        
6条回答
  •  一整个雨季
    2020-12-24 02:20

    Like other answers, I am also hoping someone smarter can point out why the compiler isn't able to infer that T is Boolean.

    One way to help out the compiler do the right thing, without requiring any changes to your existing class/interface design, is by explicitly declaring the formal parameter's type in your lambda expression. So, in this case, by explicitly declaring that the type of the value parameter is Value.

    void test() {
      Foo.foo((Value value) -> true).booleanValue();
    }
    

提交回复
热议问题