Skip to content

Different behavior in web and VM when casting int to double #47192

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

Closed
talski opened this issue Sep 10, 2021 · 1 comment
Closed

Different behavior in web and VM when casting int to double #47192

talski opened this issue Sep 10, 2021 · 1 comment
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@talski
Copy link

talski commented Sep 10, 2021

From dart-lang/language#20 and #34355

double? doubleValue;
dynamic integerValue = 1;

doubleValue = integerValue;

On windows I get type 'int' is not a subtype of type 'double?'.
On web I got no errors.

Dart SDK version: 2.14.0-383.0.dev on "windows_x64"

@lrhn
Copy link
Member

lrhn commented Sep 11, 2021

This is working as intended.

On the web, when compiled to JavaScript, all numbers are JavaScript numbers — which means that they are all doubles.

Even integers are represented by JavaScript numbers, so 1 and 1.0 is the exact same object, which is then treated as if it was an instance of a class which implements both int and double.

On native, 1 and 1.0 are different objects and instances of different classes.

The initialization dynamic integerValue = 1; treats 1 as an integer literal because it's not in a double context. That makes it an integer value on native, and the implicit integerValue as double? fails.

On the web, dynamic integerValue = 1; also treats 1 as an integer literal, which happens to evaluate to the same object as 1.0.
When you do the the integerValue as double? which is implicit in the downcast in the assignment, the web runtime system checks whether the value, 1 aka 1.0, implements double?, and it is (also) a double so the case succeeds.

@lrhn lrhn closed this as completed Sep 11, 2021
@lrhn lrhn added the closed-as-intended Closed as the reported issue is expected behavior label Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants