Skip to content

dart2js considers ints to be doubles #30001

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
trinarytree opened this issue Jun 23, 2017 · 5 comments
Closed

dart2js considers ints to be doubles #30001

trinarytree opened this issue Jun 23, 2017 · 5 comments
Labels
closed-duplicate Closed in favor of an existing report web-dart2js

Comments

@trinarytree
Copy link

in the dart vm, 1 is double is false.
but in dart2js, 1 is double is true.
according to dart's type hierarchy, int is not a subtype of double, so this seems like a dart2js bug.

here's 1 way this can manifest as trouble downstream:
someone sees a function f that returns the output of double.parse.
double.parse declares that it returns a double, but this friendly soul observes that sometimes
double.parse actually outputs an int (e.g. here not realizing that dartpad uses dart2js semantics) and so changes the return type of f to num because they were afraid that some client
might receive an int, think it's a double, and induce a bug.
this forces all consumers of f to update their code, and so on.

now int and double both extend num, and double mysteriously has no extra methods.
or, to look at it from another perspective, it's mysterious that num has methods that
seem more appropriate for doubles than all numbers, like round, truncate.
so an int can duck type as a double.
perhaps this is how dart2js can mostly get away with this,
but woe to anyone writing this code:

num numScoops({bool userLikesIcecream}) => userLikesIcecream ? 1.5 : 1;

void main() {
  var n = numScoops(userLikesIcecream: false);
  print(n is double
      ? "here's a little extra for you!"  // in dart2js, user gets confused
      : "icecream's all we got");
}
@sigmundch sigmundch added web-dart2js closed-duplicate Closed in favor of an existing report labels Jun 23, 2017
@sigmundch
Copy link
Member

@trinarytree - unfortunately this is a bug that cannot be fixed in dart2js.

Take a look at #1533 for more details.

@sigmundch
Copy link
Member

FWIW, I agree with you that this surprising behavior is not ideal, so I hope the language can be improved to highlight warn about this. For example, maybe there should be a warning if code has checks for is double and is int, since we know that they might have inconsistent semantics based on the target platform.

@floitschG @munificent - are there plans to address this in the language?

@munificent
Copy link
Member

No concrete plans that I am aware of though I personally would like to do a better job of helping users deal with cases like this.

@zoechi
Copy link
Contributor

zoechi commented Jul 3, 2017

What about a warning in dart2js about using is double or is int?

@munificent
Copy link
Member

I thought it had that warning, but maybe it disappeared at some point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-duplicate Closed in favor of an existing report web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants