0

For instance if I wanted to declare similar lambda to the following method:

boolean isXyz(String a, int b, boolean c) {
...
}

Trying

Function<String,Integer,Booleean,Boolean> isXyz = ... 

Results in a compilation error:

X.java:3: error: wrong number of type arguments; required 2
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 1
    `Function` is just a convenient interface in the standard Java library. You can always make your own if you want more flexibility, just the same way you can make more classes. – Charlie Armstrong Mar 02 '21 at 23:20
  • 1
    That first answer in the dupe is so rambly... Just copy the first bit of this one https://stackoverflow.com/a/19649473/1898563 – Michael Mar 02 '21 at 23:22
  • @Michael the problem is I would have to to "pre" declare the interface, but if I want to use it right there the first answer it works fine, e.g. say `(String,String) -> String` would be `Function> f2 = x -> y -> x + y` Ugly as hell but "works" .. and then `val result = f2.apply("hello").apply("world");` – OscarRyz Mar 02 '21 at 23:39
  • Hard to say what you should do because you really haven't given us any context. That might technically work but I'd say it's not likely to be idiomatic java – Michael Mar 02 '21 at 23:47
  • Oh I know... I know the Java way to add two strings would be `String f(String a, String b) { return a + b;}`. I was more in the way to learn the capabilities of lambdas – OscarRyz Mar 03 '21 at 00:05
  • Added my answer there with what I've learnt https://stackoverflow.com/a/66448961/20654 – OscarRyz Mar 03 '21 at 00:30

0 Answers0