4 ms·
Automatic type casting remains popular because it reduces the syntactic and cognitive overhead. I'm suggesting that a better solution would be to retain explici
by Pitarou 12y ago
Automatic type casting remains popular because it reduces the syntactic and cognitive overhead. I'm suggesting that a better solution would be to retain explicit casting, but minimise the overheads.
So, to take things a step further, lets use, say, the $ operator for casting. Then we have:
>>> x = 1
>>> y = "2"
>>> print($x + y)
3
>>> print(x + $y)
"12"
>>> print($x + $y)
CastError: ...
What do you think?
- mateuszf 12y agoPython example with str and int are IMHO much better. They require only little more typing, but are very readable and explicit.
- danbruc 12y agoThe proper way to do this is using something like TryParse() and ToString() and to handle the case of failing to parse the string as a number and maybe even specifying the locale for both operations. If you are absolutely sure that parsing can not fail you can use Parse() and omit the error handling.