-
Notifications
You must be signed in to change notification settings - Fork 213
Null Safety requires an explicit cast when casting a num to an int #971
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
I believe the analyzer is correctly implementing the language specification here, since the declared return type of |
Yes the analyzer is following the spec here, the issue of no implicit downcasts with NNBD makes code that was clean before to start issuing warnings and people might go and fix their code by adding explicit casts that could potentially have performance implications. |
I'll move this to the language repo since the "area-language" tag is marked deprecated; new issues should go to that repo. |
@lrhn I know you had some thoughts about this. Do you think it's worth trying to specify something better here? I'm pretty maxed out at the moment. |
I know that implicit vs explicit casts have a difference in dart2js depending on the flags, my understanding is that omitting implicit casts is a performance optimization that introduces potential unsoundness. Is there any documentation that expresses why an implicit cast has better performance than an explicit cast on the VM? Does it introduce potential unsoundness there? |
Whether there's a performance issue or not, it's definitely a pain point. And if we can soundly eliminate the need for the cast (e.g by encoding into static analysis the fact that int.remainder(int) is in fact an int), then there's an overall win. |
We have actually discussed some ideas for features of that nature, for instance case functions (#148): class int implements num {
...
num remainder(num other) {
int case(int i) => ...;
double case(double d) => ...;
default => ...; // Implicitly gets signature `num Function(num n)`.
}
} The point is that the cases are part of the signature of the function, and a call site can commit to a specific case, so if we call |
We have decided that |
When migrating the below code to NNBD using pkg/dev_compiler/tool/check_nnbd_sdk.dart tool, the analyzer produces a warning (requires an explicit cast)
It requires the user to do an explicit cast which can be inefficient.
We need a mechanism that would not require the user to make an explicit cast here.
The text was updated successfully, but these errors were encountered: