0

It was my understanding that is type testing operator in Dart should return "True if the object has a certain type."

So why is this happening in DartPad??

void main() {
  
  // as
  // is
  // is!
  
  double d = 3.0;
  bool result = d is int;
  print(result); // prints true!
  
}
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
  • Its due to how [numbers on represented in dart](https://dart.dev/guides/language/numbers). See [this diagram](https://dart.dev/assets/img/number-platform-specific.svg). "An `int` on the web is represented as a double-precision floating-point value with no fractional part. In practice, this works pretty well: double-precision floating point provides 53 bits of integer precision. However, `int` values are always also `double` values, which can lead to some surprises." – mmcdon20 Dec 18 '22 at 01:14
  • I guess this `is` don't check the parameter type but check the type that value can be. – Sittiphan Sittisak Dec 18 '22 at 01:22
  • 1
    Additionally: "[On the web, the underlying `int` type is like a subtype of `double`: it’s a double-precision value without a fractional part. In fact, a type check on the web of the form `x is int` returns true if `x` is a number (`double`) with a zero-valued fractional part.](https://dart.dev/guides/language/numbers#types-and-type-checking)" – mmcdon20 Dec 18 '22 at 01:28

0 Answers0