Sdk: int class has isNegative but not isPositive

Created on 8 Jul 2018  路  3Comments  路  Source: dart-lang/sdk

A certain level of consistency helps a language to be more easily learnable.

The int class has pairs isEven/isOdd and isFinite/isInfinite.

But it does not have a corresponding isPositive for isNegative.

I propose that the method isPositive be added. As a new Dart user, I just tried using this property, while trying to make some of my code read more clearly, thinking it would be there due to the existence of the other complementary methods above.

area-library library-core type-enhancement

All 3 comments

Seems like an oversight. The CL which added isNegative also lists isPostive in the description, but it didn't actually add it.

There is one catch with isPositive - it's not clear what the answer is for zero.
I think the isNegative was added mainly to support double, not int, because it allows distinguishing -0.0 from 0.0. That also suggests that the zero integer should be considered positive, if -0.0 is considered negative.

I don't personally find .isPositive more readable than >= 0, precisely because it's not clear from the isPositive name what happens for zero.

I agree that >= 0 or > 0 is clearer than .isPositive(). The only reason I imagined there was an .isPositive() is that the method-suggester in my editor had showed me an .isNegative().

On reflection, to encourage clearer code in the world, I'd withdraw this issue. And also suggest that perhaps .isNegative() is deprecated in the future.

I'll leave this open as a request to deprecate/remove isNegative. The problem with that is that it has a real use-case for doubles that is not available in any other way. Even the sign getter just returns -0.0 for -0.0, so it doesn't help you find the actual sign bit.

Was this page helpful?
0 / 5 - 0 ratings