In rust we can do something like this:
trait MyTrait { fn my_func(); } struct MyStruct {} impl MyTrait for MyStruct { fn my_func() { // this gets invoked } } fn my_func<B: MyTrait>() { // This here B::my_func(); } fn main() { my_func::<MyStruct>(); }In java we can't do something like this:
interface MyInterface { public static void myFunc() { // This gets invoked } } class MyClass implements MyInterface { public static void myFunc() { } } public class Main { static <T extends MyInterface> void myFunc() { // This here T.myFunc(); } public static void main(String[] args) { Main.<MyClass>myFunc(); } }
Now there are other questions about why java doesn't allow overriding/enforcing of static methods in java, but does this specific example not work because of monomorphization versus type erasure? I'm trying to understand the deeper reason why. My guess is that since there's only one implementation after type erasure, and static invocations need to be compiled in, it doesn't work. Is this the actual reason?