Cies
09/27/2024, 11:55 AM"some.package.ControllerName.actionMethod"
we now use ControllerName::actionMethod
: type safe, refactor friendly, less prone to bugs, and just plain stupid that this is still (2024!) not possible in Java. Thanks to whomever got this right in Kotlin, I enjoy my working life on the JVM a lot better since. 💚💛❤️Klitos Kyriacou
09/27/2024, 12:09 PMDaniel Pitts
09/27/2024, 3:18 PMCies
09/27/2024, 3:48 PMMap.of(MyClass::stop, "stop method");
Which I can do in Kotlin. (KFunction, I loooooove it)
Also, I'm not sure how SAM or Lambda will help me here...
@Klitos Kyriacou @Daniel PittsDaniel Pitts
09/27/2024, 3:52 PMMap.<Consumer<MyClass>, String>of(MyClass::stop, "stop method");
Daniel Pitts
09/27/2024, 3:53 PMDaniel Pitts
09/27/2024, 3:53 PMCies
09/27/2024, 3:54 PMDaniel Pitts
09/27/2024, 3:55 PMpackage com.stochastictinkr;
import java.util.Map;
import java.util.function.Consumer;
public class Examples {
static class MyClass {
public void stop() {
System.out.println("Hello from stop");
}
}
public static void main(String[] args) {
Map.<Consumer<MyClass>, String>of(MyClass::stop, "stop method");
}
}
Cies
09/27/2024, 3:55 PMDaniel Pitts
09/27/2024, 3:56 PMMap<Consumer<MyClass>, String> map = Map.of(MyClass::stop, "stop method");
also works.Daniel Pitts
09/27/2024, 3:56 PMimport java.util.function.Consumer;
Cies
09/27/2024, 3:57 PMCies
09/27/2024, 3:57 PMDaniel Pitts
09/27/2024, 3:57 PMCies
09/27/2024, 4:04 PMDaniel Pitts
09/27/2024, 4:07 PMCies
09/27/2024, 4:43 PMimport java.util.Map;
import java.util.function.Consumer;
public class Example {
static class MuhClass extends Example {
public void something() {
System.out.println("Hello from stop");
}
}
static class MyClass extends Example {
public void somethingElse() {
System.out.println("Hello from stop");
}
}
public void test() {
Map.<Consumer<Example>, String>of(MyClass::somethingElse, "1", MuhClass::something, "2");
}
}
Please dont put too much time in this, we already have a KFunction-based solution we're happy with. The Java method refs were not sufficient for what we wanted. I tried (to no avail):
Map.<Consumer<?>, String>of(MyClass::somethingElse, "1", MuhClass::something, "2");
Daniel Pitts
09/27/2024, 4:52 PMRunnable
instead, and use the object method reference: myObj::something
and muhObj::somethingElse
Daniel Pitts
09/27/2024, 7:19 PMCies
09/30/2024, 8:30 AM"some.package.OurClass.methodName"
) is a code smell, or better a language smell (a language should facilitate type-safe refs to methods that can be passed as values). Do you know anyplace where I can read more on KFunction being a code smell?udalov
c.get((Consumer<MyClass>) MyClass::stop)
Daniel Pitts
09/30/2024, 4:44 PMDaniel Pitts
09/30/2024, 4:46 PMCies
11/04/2024, 11:01 AMpackage.ControllerClass.actionMethod(...)
as handles for the route. The framework dictates that these references are of type String
and contain the reference as such: ``package.ControllerClass.actionMethod``
This breaks type-safety: you can change the name of the method and suddenly using the reference will result in an error AT RUNTIME. So we wrote a thin wrapper around it so we can use KFunction<*>
instead of String
to refer to these routes. These KFunction
arguments are translated to String
before they are fed to the router, nothing changed from the persective of the router. But we now refer to an action method with ControllerClass::actionMethod
and no refactorings work, CTRL-click works, and everyone is happy.
I could not achieve this with Java. Hence we love KFunction
.