4 ms·
In php, this is also true or false if you have strict types on. I’d argue that there are some valid use cases for “5” and 5 to be equal. For example, if you ta
by withinboredom 5y ago
In php, this is also true or false if you have strict types on.
I’d argue that there are some valid use cases for “5” and 5 to be equal. For example, if you take user input where on an admin screen for a user ID or username, you don’t know if the type is a string or number. Another common example is when you don’t have a known encoding for a packet yet, so you treat it as a binary string. In these cases, it’s easier to grok, as a human what is expected.
- Koshkin 5y ago> it’s easier to grok, as a human People usually understand the difference between "cat" and a cat.
- lexicality 5y agoIf you're dealing with user input, you should always be validating the type before working with it. If you want to compare input to an int, you should be verifying the input is a valid int before comparing it. Just hoping the language will do the right thing is a great way to inadvertently allow exploits in your service
- tyingq 5y agoThere are several languages where this sort of type coercion exists. Javascript, Awk, Perl, Tcl, and PHP as you mentioned. For the most part, the developers in those spaces seem to know the pitfalls and how to avoid them.
- smnrchrds 5y agoSo that's why some websites fail to log you in with numerical usernames and/or passwords that start with zero.
- llimos 5y agoPHP also has a nice thing I wish more languages had, which stops you shooting yourself in the foot due to this - the concatentaion operator (.) is different from addition (+). So "5" + 1 is 6, just as 5 + 1 is 6.
- perl4ever 5y agoI can't think offhand of a language that does overload + for strings by default. Perl uses . Oracle SQL uses || Microsoft SQL and VBA use & C obviously doesn't use +. Neither does Pascal. Power Query doesn't.
- Zababa 5y agoJavaScript, Python, Ruby, Go all use +.
- Frondo 5y agoC# does, as do quite a few others. Heh, this is kind of interesting: https://www.rosettacode.org/wiki/String_concatenation https://www.rosettacode.org/wiki/String_concatenation
- rspeele 5y agoC#, GoLang, Javascript, Python, and Ruby are several. Python and Ruby even overload *, for the dubiously useful construction: "hello" * 3 == "hellohellohello"
- infogulch 5y agoI like Julia's standard string concat which subverts a Python user's intuition: "string1" * "string2" == "string1string2". Why? Because monad.
- dahfizz 5y agoJava and Python, from the top of my head, allow "str1" + "str2"
- poizan42 5y ago> Microsoft SQL and VBA use & Nope, Microsoft SQL uses +. * https://docs.microsoft.com/en-us/sql/t-sql/language-elements/string-concatenation-transact-sql?view=sql-server-ver15 https://docs.microsoft.com/en-us/sql/t-sql/language-elements... * https://i.imgur.com/7z81o8t.png https://i.imgur.com/7z81o8t.png * https://i.imgur.com/VDR9qer.png https://i.imgur.com/VDR9qer.png > C obviously doesn't use +. But C++ does for std::string. > Neither does Pascal. Original Pascal didn't, but I think most modern Pascals do (e.g. Object Pascal known from Delphi).
- crote 5y agoOn the other hand, what about "V" == 5? Or one of the literally dozens of unicode characters representing the number five in different writing systems? Maybe even "five" == 5? Accepting both a numerical user id and a username string is a very valid scenario. However, I believe you should handle that by explicitly trying to parse it as a number, not hoping that automatic coercion will do the right thing. It's just a footgun waiting to go off.
- tetha 5y ago> Accepting both a numerical user id and a username string is a very valid scenario. However, I believe you should handle that by explicitly trying to parse it as a number, not hoping that automatic coercion will do the right thing. It's just a footgun waiting to go off. This is why many people recommend questioning the use of basic or naked types. Details vary between the language, but this should be a type UserID with a UserID::from(String) and maybe a UserID::from_symbolic_name(String) and everything but some edge code should work on UserIDs. This makes code easier to read, too.
- infogulch 5y agoGood point. "5" == 5 "⑤" == 5 "⑸" == 5 "⁵" == 5 "𝟝" == 5 "𝟓" == 5 "𝟻" == 5 "V" == 5 "Ⅴ" == 5 "𝍸" == 5 # https://unicode-table.com/en/1D378/ "𐄋" == 5 # https://unicode-table.com/en/1010B/ "৫" == 5 # https://unicode-table.com/en/09EB/ If you think the first should work, you should justify why the others shouldn't.