-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow to compare generic class types with == operator #1326
Comments
You should be unblocked by a function like Type typeOf<X>() => X;
void main() {
print("List<int> != int: ${typeOf<List<int>>() != int}");
} Terms like Your helper comparison function will work as well, but the |
Cf. #123. |
@eernstg ok, so I got 3 points in my issue, one of them is tracked by another issue, 2 are remaining:
|
The I'm not sure what numbers 4 and 5 mean. |
I think it might be helpful to spell out the actual actions taken at run time: With We don't specify the nature of the run-time entities used to determine whether or not the subtype relationship Instances of You can find some documentation about the
Let's say we wish to test against // Assuming null safety.
void f<X>(X x) {
if (x is List<num>) {...} // 4.
}
class C<X extends Object> {
X x;
void f() {
if (x is List<num>) {...} // 5.
}
} These tests are unsafe: |
@tatumizer obfuscation will fail that code |
don't see why |
@nt4f04uNd, I think we covered the two remaining issues at this point, so I'll close the issue as a duplicate of #123. Please create a new issue if you think some parts still need further discussion. |
I'll talk about this feature in context of the problem I faced in my app.
The problem
At some point I needed to runtime check the generic type of my class in its method.
Assume we have a code something like this
Solutions
I started digging into how to do that and found several ways:
is
operator on types==
operator on types1.
is
operator on typesAs far as I can understand,
is
operator is not what I need and what it does is that it compares the variable type to the given type, and in case of comparing two types, it will always give me false. That's okay, but this operator is not documented fairly and I would encourage dart team to do that.2.
==
operator on typesWhen I use the
==
all goes fine until I decide to use some generic type in it, for exampleT == List<String>
. This syntax is just not allowed for some reason. Enabling it is the feature I'm requesting for.3. Helper comparison function
Just a simple function as this will always compare the types as I need, even generic ones
Some other weird ways of doing this:
is
, but it's not viable in cases when method doesn't have any argumentsis
Interesting thing about these two ones that the first will work even if agrument is null, but the second one won't. I would be glad to hear some explanations here on why is that aswell.
Example
In the code example below there are illustrations on how this works/not works all methods, except the
5
one.The text was updated successfully, but these errors were encountered: