-
Notifications
You must be signed in to change notification settings - Fork 1.7k
There's no good way to promote an argument #35524
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
My first recommendation is (as usual) to not use The If the third example is correct and void foo(Object bar) {
if (bar.runtimeType == this.runtimeType && bar is Bar<T, R, S>) {
...
}
} That 's the only way to promote an existing variable. Otherwise you need to introduce a new variable like you did in the first example. |
As far as I can tell, It's not always Also, in general, the |
This is only sometimes going to be true in aggregate. The I do think that providing a way to do what you're trying to do more succinctly and in a way that both the analyzer and the compiler can take advantage of would be a good thing though. |
Sure. This bug is about the cases where it's true.
Or we could crash, that'd be fine too. That's what would happen in C++, for example. Anyway, I'd be fine with release builds having a very cheap version of the assert. We could even have dedicated syntax for this kind of check. alwaysAssert(foo is Bar); In debug builds (if false) it could throw and so on, in release builds it could just abort the program. |
Or maybe: promote foo as Bar; |
There is currently no way to derive type promotion from If you want promotion, you will have to do an actual type check. Whether you do: Subtype newVar = oldVar; or if (oldVar is Subtype) { ... } is a matter of taste (the former is more efficient when compiled to JavaScript in the unsafe "trust type annotations" mode, should be completely equivalent on the VM). In the future, we hope to improve on the cases that can cause type promotion. I'd hope for Crashing is not an option. The C++ crash is due to type unsoundness, and it doesn't have to crash, it can just do arbitrary memory corruption instead. The current Dart compiler does more checking than it needs to, because it still doesn't trust types completely (the legacy of Dart 1), but it should be possible to write a Dart compiler which makes assumptions about the memory layout of objects, without repeatedly checking that the object has the right type. Allowing an incorrectly typed object to flow into such code would be a security issue, and since we don't want that, it would preclude making such optimizations. |
We could also just ban overrides of |
I personally really like the idea of making runtimeType an extension method
on Object that is implemented in a private way.
…On Mon, Mar 4, 2019, 6:40 PM Ian Hickson ***@***.***> wrote:
We could also just ban overrides of runtimeType. Combine this with
proposals elsewhere to add post-fix "fake methods" like .as, and define
.runtimeType as one of these fake methods. Or combine it with the
proposal to introduce sealed methods, and seal it. Or just special case
runtimeType.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#35524 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAKQ7mGLdDcc86xJFRY9ckHCsraTHzjwks5vTdkkgaJpZM4ZlETN>
.
|
(This bug assumes a world with
implicit-casts: false
.)If I have a method which takes an argument of unknown type, there's currently no clean way to write the code that handles that argument in a type-safe way, as far as I can tell.
What is the preferred way to write this code?
This comes up all the time e.g. in
operator ==
implementations, or when handling Json data or other structured data parsers like the StandardMessageCodec logic in Flutter.The text was updated successfully, but these errors were encountered: