strconv: raise error if not a number #11684
Replies: 20 comments 10 replies
-
What's your say on this? @penguindark |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
But I agree, atoi() should return |
Beta Was this translation helpful? Give feedback.
-
"number := os.input('enter number').int()" |
Beta Was this translation helpful? Give feedback.
-
if we add an optional for so |
Beta Was this translation helpful? Give feedback.
-
@Delta456 No, it just returns 0 if the number is invalid, like |
Beta Was this translation helpful? Give feedback.
-
@TheBrainScrambler I meant if we will add an optional so what will it return. |
Beta Was this translation helpful? Give feedback.
-
@Delta456 I don't understand, the optional should return 0 if it isn't an error ? What ? I also discovered by using the |
Beta Was this translation helpful? Give feedback.
-
Yeah I'm for making pretty much all user-interaction functions return an optional type. There should be no overlap between valid returns (like 0 in this case) and invalid returns (also 0 in this case). This seems like the perfect use-case of optionals. |
Beta Was this translation helpful? Give feedback.
-
@medvednikov @Delta456 |
Beta Was this translation helpful? Give feedback.
-
@penguindark C does not have optionals, so returning 0 is the best solution within the constraints of the language. V has a feature that would lend itself perfectly for parsing "iffy" data (unknown strings) with the certainty that whatever is returned by the function, if not an error, is precisely what the developer wants. Without optionals, there is no way to know if the user typed 0 or if they typed nonsense. |
Beta Was this translation helpful? Give feedback.
-
It would perhaps be better to create a different method with a new name, instead of changing the way atoi works. The official documentation for the parameter to atoi (in C, C++, etc.) is: C-string beginning with the representation of an integral number. The key part (that I bolded) is "beginning with". This also means that atoi will return 5 if you pass it a string of "5ive", instead of giving an error. So having a separate strict version of atoi would be ok, but changing the way atoi works would just make porting existing code harder. |
Beta Was this translation helpful? Give feedback.
-
I agree with JalonSolov |
Beta Was this translation helpful? Give feedback.
-
I don't know if the |
Beta Was this translation helpful? Give feedback.
-
Looks like .int()... example line:
|
Beta Was this translation helpful? Give feedback.
-
Then I think the .int() method should be modified to return an Optional and keep strconv.atoi for C compatibility. This should also be marked as a bug. I can't properly use the CLI library because if an user enter some string on an int flag, it won't raise an error and instead return 0. But 0 is also a valid entry so I can't check if the entry is 0 ... |
Beta Was this translation helpful? Give feedback.
-
Yeah good point |
Beta Was this translation helpful? Give feedback.
-
Can this issue be closed? |
Beta Was this translation helpful? Give feedback.
-
As a newcomer to the language, I was extremely surprised to find that the introduction of As this issue is not closed, perhaps the behaviour can still be changed and vfmt used to invert the use of one with the other. If so, atou32, and friends may need to be added to vlib to allow a full substitution. |
Beta Was this translation helpful? Give feedback.
-
Indeed, So, |
Beta Was this translation helpful? Give feedback.
-
Hello, I've just discovered the language and have read the docs. Now I want to practice, so I'm writing a little guessing game where you enter a number between 1 and 100.
I need to convert the user's input from a string to an int. Reading the docs, I've found https://modules.vlang.io/strconv.html#atoi which does what I want. BUT ... not exactly.
If I enter some random characters in my program,
strconv.atoi
will just convert it to 0 instead of rising an error where it says that the string can't be converted.I think this is incorrect behavior, the strconv methods should be Optionals, or at least provide methods which implements this mechanisms and are Optionals.
TL;DR
Expected behavior: If I do
strconv.atoi("blablah")
it should let us handle the error with an OptionalCurrent behavior: It just returns 0 ...
Beta Was this translation helpful? Give feedback.
All reactions