r/lolphp • u/i_make_snow_flakes • Nov 15 '14
new safe casting function RFC. casting "-10" to string is valid but casting "+10" is not..
here the comment where one user asked the author of RFC about this. I am not able to follow his reasoning. What do you think?
22
Upvotes
View all comments
Show parent comment
1
u/midir Nov 16 '14 edited Nov 16 '14
+1parses as a valid integer, but the+is not part of the integer literal; it is a unary plus operator attached to the integer literal. Note the difference here when there is arbitrary whitespace between the+operator and number:The difference is because whitespace is allowed in the source, but not by the string-to-int conversion, and other languages are the same in that regard.
Likewise:
My point is that merely because
+1is valid source code is not in itself a reason why it should be accepted by a general string-to-int conversion. Anther difference between source parsing and general string-to-int conversion is the treatment of leading zeros: being treated as octal in source but as decimal by string-to-int conversion.