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
3
  • 1
    Seems that the runtimeType adapts to the value. z += 0.1; foo(z); gives 2.1 runtimetype: double May 13, 2017 at 4:41
  • 1
    Are you running on the VM or via dart2js?
    – Seth Ladd
    May 13, 2017 at 4:50
  • @SethLadd These results were obtained on dartpad, but we also see weirdness in Chrome (so I think dart2js was involved).
    – Brian Neal
    May 13, 2017 at 12:49

1 Answer 1

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.

7
  • 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, 2017 at 14:52
  • Dartpad is using dart2js to generate JavaScript. 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. May 13, 2017 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, 2017 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. May 13, 2017 at 15:03
  • 1
    I incorporated your comments into the answer. Please edit if you wish.
    – Brian Neal
    May 13, 2017 at 15:10

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.