-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
"is" operator should support an expression on the right hand side #27680
Comments
Right now we work around this using generics: |
This was discussed by the language team earlier this year, and the sentiment seemed broadly favorable. I don't think we will be able to get to it this quarter though. |
I'm curious - does this have potentially expensive cost for say, dart2js? One reason I bring this up is we are constantly check compiled output and trying to avoid reified type information in production. As far as I know ( @rakudrama @hterkelsen ), dart2js does a decent job today of removing reified type information when it's not used (i.e. no Would this language feature mean dart2js defensively has to hold all reified type information? |
I don't think it should make any difference at all, but @rakudrama can weigh in. You already have |
But |
@matanlurey you just look at which type literals are created in the program - that gives you a conservative approximation of all possible values of |
But I want a liberal optimization not a conservative approximation :( |
No. Counter-example: bool check<T>(var x) => x is T;
void test() {
check<FooBar>(3);
} There is no static occurrence of |
Ah, that's fair. I guess I'd just be concerned about language features that make it too hard to optimize, but if you're not concerned I guess I'm not concerned either 🤣 |
dart2js perspective: TL;DR: I'd need to read a spec before trying to determine if this would be bad for dart2js. Does this feature carry it's weight? The I would be concerned about many other ideas like this that would feel natural is x is t was supported. We would have to do a lot of work with the dart2js runtime representations to support x is t but it should not lead to extravagant bloat. We would not consider working on this until after completing the kernel transition. What is true of the current analysis is that we find a relatively small number of types from type expressions in constructor type parameters flow to a context like x is T. This is usually much smaller than the number of type literals, thousands of which are used as keys in Angular injection. Since the current type bindings are restricted in how they work and sparse within the program text, the flow can be computed precisely and quickly. This is not true of general values. How would this actually work? dynamic t = ...
print(1 is t); Would it be permissible to implements Type and provide an operator is method? |
The most recent case I'm aware of where Flutter would have liked this is that we have a assert(map[key] is key); ...but today the best we can do is: assert(map[key].runtimeType == key); ...which isn't enough because the instance might be a subclass of |
This should be possible:
Instead you get a runtime exception ("malformed type: line 4 pos 15: using 't' in this context is invalid") and an analyzer error ("The name 't' is not a type and cannot be used in an 'is' expression").
The text was updated successfully, but these errors were encountered: