[v2] IsDigit to return True for positive Integers

Propose new features and changes
Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

[v2] IsDigit to return True for positive Integers

Post by Descolada » 07 Jan 2023, 15:35

This is either a wish to change IsDigit behavior, or a wish for documentation improvement:
Currently the docs for IsDigit says that it returns "True if Value is a positive integer, [...]", but also that "Value must be a string, otherwise a TypeError is thrown.". IsDigit(5) throws an error, so currently positive pure numeric values are not allowed.
In the source for script2.cpp it's commented that pure numeric values shouldn't be allowed for funcs other than IsNumber, IsInteger, IsFloat because the results would be unintuitive, but in my opinion it would be intuitive for IsDigit to be True for pure numeric values of >=0.

Edit: On further thought I wouldn't be opposed for IsAlnum to have the same behavior. As an user I would like for these functions to answer the question I'm asking as easily and clearly as possible, and not be fussy about the types of variables passed to the function. The question with IsDigit is that "does this value consist on only digits from 0-9?", and the answer should be for IsDigit("5") and IsDigit(5) both to return True since both consist of digits from 0-9, and I don't care whether one is actually a string or pure numeric value behind the scenes. If I did care about the type, I would use IsInteger.
With IsAlnum the issue seems a bit more complex, since the "alpha" part implies we might want to be dealing with strings instead of pure numeric values. But still, the question is whether the argument contains only numbers and letters, and the answer for IsAlnum(5) should still be True since a non-negative integer contains only numbers.

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: [v2] IsDigit to return True for positive Integers

Post by lexikos » 16 Jan 2023, 22:55

That was probably a remnant from a previous alpha. IsDigit and related functions require a string because otherwise, it is likely that some users will be confused by hexadecimal literals producing decimal strings (String(0x10) -> "16").

5 is not an alphanumeric string and does not contain digits. If you want to know whether a value is numeric, use IsNumber. If you want to know whether a string is composed of digits and alphabetic characters, make sure you actually have a string in the first place.

Descolada
Posts: 1077
Joined: 23 Dec 2021, 02:30

Re: [v2] IsDigit to return True for positive Integers

Post by Descolada » 27 Jan 2023, 00:25

@lexikos, I think that users who are capable of understanding and using hexadecimal numbers are also able to figure out that a hexadecimal number gets converted into a 10-based one. My complaint is similar to this one, and I tend to agree with them that perhaps the IsXXX functions should not throw on non-string types at all and instead return 0. I think the problem here is the naming of IsXXX functions: they deceptively suggest that they should accept any types of numbers, because otherwise it would be named differently. StrLen obviously requires a string as an argument, whereas IsInteger accepts any type and even converts a string into an integer for the check! Thusly, StrIsAlpha would obviously require a string, "mystring".IsAlpha as well, but IsAlpha should accept any type.

If you are gonna keep it the way it is, it would be nice to have IsString complementary to IsInteger and as an alternative to var is String, since finding the latter from the docs will probably be rather problematic for newer users. Searching the docs for IsString returns nothing, Is returns the Is Functions page which doesn't have an answer, and the fourth result is has a complicated explanation that requires a newbie to understand that String is a Class.

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

Re: [v2] IsDigit to return True for positive Integers

Post by iseahound » 30 Jan 2023, 10:35

I think users would expect IsDigit to automatically return true for a numeric value. Truthfully it's only the naming that feels off to new users.

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [v2] IsDigit to return True for positive Integers

Post by just me » 30 Jan 2023, 10:49

Though I agree that it might be confusing at first when you use the string related Is-Functions:
A DIGIT is a character class. Strings are stored as characters, pure numbers not.

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

Re: [v2] IsDigit to return True for positive Integers

Post by iseahound » 30 Jan 2023, 10:55

The user expects a check for positive integers which is a source of confusion. I'm not sure how to resolve it, but maybe IsStringDigit would make it more clear. Since the new user doesn't really differentiate between a string and a number (due to the implicit type coercion) it is a little odd that it would not happen here where as it would occur here var ~= "\d+".

Post Reply

Return to “Wish List”