fixnum icon indicating copy to clipboard operation
fixnum copied to clipboard

Remove operator== considering Int64 == core.int

Open matanlurey opened this issue 6 years ago • 8 comments

We have a work-around in place for the linter, but this seems not worth it at all:

  • It makes comparisons brittle (int640 == int0) but (int0 != int640).
  • It only seems useful for tests (and we could just create a custom Matcher)
  • It is confusing, I don't know of any other class that does this in the Dart ecosystem

/cc @davidmorgan

matanlurey avatar May 17 '18 17:05 matanlurey

I think removing that is likely to break Intl's ability to format Int64. We rely on being able to ask things like someNumber.remainder(1) == 0

alan-knight avatar May 17 '18 17:05 alan-knight

So let's just special case it? Seems silly here. We don't have the same treatment of BigInt for example.

matanlurey avatar May 17 '18 17:05 matanlurey

Special-casing it in Intl means adding a dependency on fixnum, which I'd prefer not to do.

Not doing this makes BigInt less useful. For example, you can't format it with Intl. At least BigInt we could special-case without adding a dependency because it's in the SDK.

alan-knight avatar May 17 '18 17:05 alan-knight

Sounds reasonable still. This library is very lightweight and does not change much. It's a higher mental cost to have this behavior than worry about a dependency on fixnum.

matanlurey avatar May 17 '18 18:05 matanlurey

By far the most common current use is to check whether an Int64 is equal to 0. This does make the code look nicer, and people are using it.

Once the lint is updated, it will tell you if you do the comparison the wrong way around. At that point, there doesn't seem to be a lot of downside to the feature.

I wouldn't have designed it this way to start with, but I'm not sure it's worth a breaking change at this point.

davidmorgan avatar May 18 '18 07:05 davidmorgan

This "feature" violates the == contract (https://api.dartlang.org/stable/2.0.0/dart-core/Object/operator_equals.html), since

void main() {
  print(4 == Int64(4));
  print(Int64(4) == 4);
}

prints

false true

so I think it's pretty clear it should be removed.

robertoscaramuzzi avatar Aug 31 '18 23:08 robertoscaramuzzi

I agree. FWIW, I'd be willing to help with the migration internally if we make this change.

matanlurey avatar Sep 03 '18 16:09 matanlurey

There is also the larger question of whether it is worth keeping Int64 now that we have BigInt.

matanlurey avatar Sep 03 '18 16:09 matanlurey