Not quite. As the previous commenter said, every string has at least one string representation (i.e. 100 -> “100”, “1e2”). So there’s no sensible way to write a pure function handling that, you’re just cooked no matter what you do.
Implicit cast from number to string, explicit parsing in the other direction.
For comparisons as number, parse the string to a number and compare two numbers.
The main point of the implicit cast from number to string is to concattenate number to string, e.g. print("testValue: " + testValue).
Another option (e.g. taken by Python) is to acknowledge that there’s no pure 1:n mapping in any direction between string and number, so any conversion between those two needs to be done explicitly. That’s probably the most correct implementation, but it makes string concattenation annoying. But then again, f-strings and similar techniques make that problem pretty much obsolete.
Not quite. As the previous commenter said, every string has at least one string representation (i.e. 100 -> “100”, “1e2”). So there’s no sensible way to write a pure function handling that, you’re just cooked no matter what you do.
Other languages handle that easily:
Implicit cast from number to string, explicit parsing in the other direction.
For comparisons as number, parse the string to a number and compare two numbers.
The main point of the implicit cast from number to string is to concattenate number to string, e.g.
print("testValue: " + testValue)
.Another option (e.g. taken by Python) is to acknowledge that there’s no pure 1:n mapping in any direction between string and number, so any conversion between those two needs to be done explicitly. That’s probably the most correct implementation, but it makes string concattenation annoying. But then again, f-strings and similar techniques make that problem pretty much obsolete.