4 ms·
First-class methods would be a win, but the syntax leaves a little bit to be desired. Closures would be much more of a syntactic eyesore if they A) had context
by akeefer 17y ago
First-class methods would be a win, but the syntax leaves a little bit to be desired. Closures would be much more of a syntactic eyesore if they A) had context-based type inference on the arguments, so that if I'm passing a closure to a method that expects (String, String) -> as its type I don't need to redeclare the variable types and B) allowed for embedded expressions rather than requiring them to be full function definitions with a "return" statement in there. Unfortunately, the FCM proposal doesn't allow either of those.
To use their example, it should be much more like:
Collections.sort(list, \str1, str2 -> str1.length() - str2.length());
instead of:
Collections.sort(list, #(String str1, String str2) {
return str1.length() - str2.length();
});
There's no technical reason you couldn't do the former in Java (and their proposal seems to infer the return type just fine?), but no one involved with the Java language seems to really value conciseness.