-
Notifications
You must be signed in to change notification settings - Fork 1.7k
X is Enum
reporting false
for enum X
#52824
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
PS I understand that
should print Why does the code in the original report even compile, if class names can't be used for static inheritance checks like that? |
Asking You can still emulate it: enum X { a, b }
void main() {
print(<X>[] is List<Enum>);
} This works because you can create an instance of |
Thanks for the explanation, but that makes me think the documentation (I guess I'm having trouble getting my mind around your |
Ah, I see what the documentation is hinting at -- it is the values, not the If that is the case, it is unfortunate that the naming is This is tricky coming from a Java backround, because in Java, the enum class itself is a class that extends Maybe the documentation could be updated to explain this a bit better? It is very terse at present. |
No, that's the whole point, For the But Dart does not support For the So if you want to compare two types in order to determine whether they are subtype related then you can't use an operator, it simply doesn't exist. But void f<X extends Y, Y>() {}
void main() {
(f as Function)<T1, T2>(); // Will throw if and only if `T1` is not a subtype of `T2`.
} In this case you are indirectly using the |
That's true in Dart, too. enum E { a, b }
Enum e = E.a; // OK, because `E <: Enum`. The |
OK, so I guess my misunderstanding was that I have a weird |
@lukehutch The code in your first comment has roughly the same semantics as this Java program, which also prints public class App {
enum X {}
public static void main(String args[]) {
System.out.println((Object)X.class instanceof Enum);
}
} I don't think Java has an operator that can check if one type is assignable to another type, just like Dart. I think the confusion might be around being able to use types as expressions (type literals) in Dart and what type such an expression has. As @eernstg mentioned, it's always They are a foot gun in switch cases (dart-lang/language#2911) and there is a proposal to remove them (dart-lang/language#2393). |
@blaugold Java has public class Main {
static class X{}
static class Y extends X{}
public static void main(String[] args) {
Y y = new Y();
System.out.println(y instanceof X);
System.out.println(X.class.isAssignableFrom(y.getClass()));
}
} output:
|
Actually it looks like Dart has the same behavior via I guess this means I'm still missing something about Dart's semantics -- but I'll close this issue, since it's not a bug. Thanks for the valiant efforts to explain this! |
prints
Huh? The documentation for
Enum
statesThis class is implemented by all types and values introduced using an enum declaration.
dart --version
): Dart SDK version: 3.0.5 (stable) (Mon Jun 12 18:31:49 2023 +0000) on "linux_x64"The text was updated successfully, but these errors were encountered: