0

I was playing around in Java
and I tried to initialize a variable with a lambda expression
however, when trying to use the variable I get the error

java: cannot infer type for local variable test (lambda expression needs an explicit target-type)

public static void main(String[] args){
    var test = (e -> {
        return 10;
    });

    Arrays.stream(new int[]{3})
            .map(test)
            .forEach(System.out::println);
    
}

Honestly, I was just trying to initialize a variable using a method
and it just sort of became lambda so I just played around with lambda

  • Try to write a function instead of var – Mazhar Ibna Zahur Aug 25 '22 at 00:57
  • 1
    A *lambda expression* **doesn't** have a type by itself, it means that its type should be inferred from the context. `var` also implies that the type of the variable `test` needs to be inferred. So you have a statement where both the right the left parts has no type. And complains because you're not giving any clue what these types are. – Alexander Ivanchenko Aug 25 '22 at 01:12

0 Answers0