-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Access of static class method by static generic method #34131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It is not possible in Dart to call static methods this way. |
Hi @artemirq, @matanlurey already mentioned that Dart static methods cannot be called as shown. However, I'm tempted to add a few more words on that. First, it doesn't actually work in Java: // Cf. SDK issue #34131
class A {
public static void foo() {
System.out.print("I <3 ");
}
}
class A2 extends A {
public static void foo() {
System.out.print("Let's see whether B also <3 ");
}
}
class B {
public static <T extends A> void bar() {
T.foo();
System.out.print("Java");
}
}
public class n000 {
public static void main(String[] args) {
B.<A>bar(); // Prints 'I <3 Java'
B.<A2>bar(); // Also prints 'I <3 Java'
}
} With
But that's not surprising—Java must ignore the actual value of Dart has reified type arguments, so The main reason why we did not do it is that there is no typing relation between the static methods of any two classes, and it makes no difference whether those two classes are subclasses or subtypes of each other—each class has its own set of static methods, and there is simply no relationship between those sets of methods. So even if we did give you the ability to do So what you are asking for does not match up with the more strict typing that Dart uses today (especially Dart 2 which was released recently, but we've been moving in that direction for a couple of years before that). Returning to the actual error that you're seeing: |
@eernstg Thank you very much for your research and detailed answer. I agree, perhaps in real-world problems call static methods by this way really not needed By the way, I tried DLang and your example works fine in this lang (unlike Java) import std.stdio;
class A {
static foo() {
writeln("A.foo");
}
}
class A2 : A {
static foo() {
writeln("A2.foo");
}
}
class B {
static foobar(T : A)() {
T.foo();
}
}
void main() {
B.foobar!A();
B.foobar!A2();
} Playground https://run.dlang.io |
Interesting! I had to find a bit of time to look into D in order to understand what on earth that piece of code is doing. ;-) However, it looks like In Dart, we could add an instance method to the |
I try call static method of class by generic method and it's not compiling with error:
Error: The method 'foo' isn't defined for the class 'dart.core::Type'
https://dartpad.dartlang.org/a94baf3ef84a6b22827a39aa4c5133b8
PS: I doesn't know, it's an issue, bug or only question, but I would be glad if it was possible
And it's normally work in Java!
The text was updated successfully, but these errors were encountered: