4

I'm trying to determine if a dynamic parameter to a function is really an int or a double and I'm finding surprising behavior (at least to me).

Can anyone explain this output (produced on dartpad)?

foo(value) {
  print("$value is int: ${value is int}");
  print("$value is double: ${value is double}");
  print("$value runtimetype: ${value.runtimeType}");
}

void main() {
  foo(1);
  foo(2.0);
  int x = 10;
  foo(x);
  double y = 3.1459;
  foo(y);
  double z = 2.0;
  foo(z);
}

The output:

1 is int: true
1 is double: true
1 runtimetype: int
2 is int: true
2 is double: true
2 runtimetype: int
10 is int: true
10 is double: true
10 runtimetype: int
3.1459 is int: false
3.1459 is double: true
3.1459 runtimetype: double
2 is int: true
2 is double: true
2 runtimetype: int
Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
Brian Neal
  • 31,821
  • 7
  • 55
  • 59

1 Answers1

3

In the browser there is no distinction possible between int and double. JavaScript doesn't provide any such distinction and introducing a custom type for this purpose would have a strong impact on performance, which is why this wasn't done.

Therefore for web applications it's usually better to stick with num.

You can check if a value is integer by using for example:

var val = 1.0;
print(val is int);

prints true

This only indicates that the fraction part is or is not 0.

In the browser there is no type information attached to the value, so is int and is double seem to just check to see if there is a fractional component to the number and decide based on that alone.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for the explanation, but your example doesn't seem to work on dartpad. Is dartpad using the VM or a javascript backend? – Brian Neal May 13 '17 at 14:52
  • Dartpad is using `dart2js` to generate JavaScript. https://dartpad.dartlang.org/29693618d1199b1bc5131a627f09a0e6 Perhaps I wasn't clear enough. Above code is supposed to print `true`. It just checks if the value has a fragment part different from 0. – Günter Zöchbauer May 13 '17 at 14:56
  • When you say there is no distinction between int and double in the browser, then why does `3.1459 is int` produce a different result than `3.1459 is double` on dartpad? – Brian Neal May 13 '17 at 14:59
  • 1
    There is different JS created for `is int` and `is double`. What I meant is that there is no type information attached to the value that allows to distinguish if the value is int or double. It seems `is int` does something similar as I did in the code in my answer. It just checks if the value has a fragment != 0. `0.0 is int` also returns `true`. – Günter Zöchbauer May 13 '17 at 15:03
  • 1
    I incorporated your comments into the answer. Please edit if you wish. – Brian Neal May 13 '17 at 15:10
  • The comment "a value is an integer" and then "1.0 is int == true" is confusing. I'm not actually checking if a value is an integer with the code written. I can, however, check if a value is a num. – Seth Ladd May 17 '17 at 03:13
  • @SethLadd that's the point of the answer. The question is about checking for `int` not checking for `num`. I'll try to make it more clear. Thanks for your effort. – Günter Zöchbauer May 17 '17 at 03:26