3 ms·
Java has (limited) type inference now I think, right? Does this work for the example you gave? public class Main { public static void main(String[] a
by saghm 3y ago
Java has (limited) type inference now I think, right? Does this work for the example you gave?
public class Main {
public static void main(String[] args) {
var add = x -> y -> x + y;
var addFive = add.apply(5);
int result = addFive.apply(3); // Output will be 8
}
}
Still not great, but a lot better than the original IMO
- danhau 3y agoNo, I don’t think that works. Last time I checked, type inference doesn‘t work for lambda expressions. Part of the reason - I think - is that lambda expressions need to be boiled down to some concrete @FunctionaInterface interface. You can have arbitrarily many of these, all with the same signature and method name, so language doesn‘t know which to choose.
- _old_dude_ 3y agoYou need an helper method for that public class Main { private static <T,R> Function<T,R> fun(Function<T,R> function) { return function; } public static void main(String[] args) { var add = fun(x -> fun(y -> x + y)); var addFive = add.apply(5); int result = addFive.apply(3); // Output will be 8 } } Those helpers should be part of the JDK.
- mumblemumble 3y agoThrow a checked exception in there and it all goes to hell all over again.