But I can't see any justification for new RegExp to indiscriminately coerce any input to a string.
You can't see any justification for a largely JIT-based language to automatically cast any input to a string when the only possible inputs the function takes are strings?
Correct. If it's not already a string, it should (logically) not be coerced into a string.
String coercion makes a lot of sense in a lot of cases, but mostly when the ultimate use of the variable is as a string.
In the case of RegExp, the goal is not to have a string like "Hello World", the goal is to have a regular expression that can be processed.
So casting any non-string to a string to a string is just asking for an infinite supply of broken edge cases.
when the only possible inputs the function takes are strings?
This part is technically wrong, but whatever. It takes regex literals and other RegExp objects, which is reasonable, but it could check to make sure the input is one of these three things and throw an error for anything else.
A big part of JS philosophy is to avoid throwing errors as much as possible, because of its initial intended uses in web pages. They simply didn't want a whole web page to break and not be rendered because someone made a type error somewhere and now nothing is displayed but an error page.
Rather, you can see most of the page fine and maybe that image that you forgot to pass properly to the element that should display it will simply show as "[object Object]" and the rest of the page looks fine.
It would honestly be better if a RegExp object that was passed a thing that can't be reasonably parsed as a regex simply always returned false when asked whether something matches it.
Better still would be error throwing with built-in error handling that allowed for things like some of your code running when other parts break.
First option is a good idea, but frankly the authors of this method probably didn't consider it that far. They just wrote the method to assume everything thrown at it would be a string (if not a regex) and the exceptions got coerced into one. Which is how most JS code will work when you don't actively handle the wrong type parameters.
Second option is still a problem, JS has error handling but you have to actively use it. Wrapping every method of your code in try-catch (or multiple try-catches, since we wouldn't want one error somewhere to break unrelated code) ends up becoming boilerplate and bureaucratic.
Being jit has nothing to do with js having insane coercion tolerance. That's purely a language design choice. And while yes I know the justification for it, imo it's absolutely not worth the cost. This is why typescript has gained prominence.
The issue is type coercion, really. I don't personally like dynamic types for other reasons (mainly poorer linting due to unknown types), but this type of shit doesn't happen in, for example, Python.
View all comments
829
u/Kashrul Mar 20 '24 edited Mar 20 '24
I know nothing about js, maybe that's why I can't see square brackets in initial question?