You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
numnumScoops({bool userLikesIcecream}) => userLikesIcecream ?1.5:1;
voidmain() {
var n =numScoops(userLikesIcecream:false);
print(n isdouble?"here's a little extra for you!"// in dart2js, user gets confused:"icecream's all we got");
}
The text was updated successfully, but these errors were encountered:
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.
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 ofdouble
, 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 adouble
, but this friendly soul observes that sometimesdouble.parse
actually outputs anint
(e.g. here not realizing that dartpad uses dart2js semantics) and so changes the return type of f tonum
because they were afraid that some clientmight receive an
int
, think it's adouble
, and induce a bug.this forces all consumers of f to update their code, and so on.
now
int
anddouble
both extendnum
, anddouble
mysteriously has no extra methods.or, to look at it from another perspective, it's mysterious that
num
has methods thatseem more appropriate for doubles than all numbers, like
round
,truncate
.so an
int
can duck type as adouble
.perhaps this is how dart2js can mostly get away with this,
but woe to anyone writing this code:
The text was updated successfully, but these errors were encountered: