Every language that supports default and variable arguments does this. This includes Python, C, and C++ and has absolutely zero to do with "loose typing" in this case.
Every language that supports default and variable arguments does this. This includes Python, C, and C++ and has absolutely zero to do with "loose typing" in this case.
But you can't!!!
> Every language that supports default and variable arguments does this.
Python:
map(float, ["1", "2", "3"])
C:No equivalent syntax.
C++
std::vector<std::string> a = {"1", "2", "3"};
std::vector<int> b;
std::transform(a.begin(), a.end(), std::back_inserter(b), std::stod); # compile error
You can say "well feature X exists elsewhere" or "library function Y exists elsewhere", but only JS makes the collective design choices that cause this phenomenon.Good, bad? IDK, that's subjective. But unique? Certainly.
If you ever, like at all, had the pleasure of using any Python library without type hints - past iterations of numpy and in particular matplotlib come to mind - you'd be blown away by the amount of
def some_function(*args, **kwargs)
Have fun trying to figure those out even with an API reference at hand. Fun times.Also C does have equivalent syntax, namely variadic functions in combination with function pointer arguments. You can design all kinds of crazy interfaces with that, too, which will exhibit strange and unexpected behaviours if used incorrectly. Heck, name a single C noob who didn't cause a segfault while trying to read a number using sscanf().
When it comes to stdlib interface design, C is actually much more offensive than JS :) strtok() comes to mind. More footguns than stars in the sky just in that innocent seeming function alone. And don't even get me started with the C++ STL...
So no, it's not just JS that makes design choices that seem odd and unintuitive - you'll find them in every language that is actually in widespread use and "battle tested". It's funny to me how some people try to single out JS in that regard, even though it's in no way special when it comes to this.
> help(int)
class int(object)
| int([x]) -> integer
| int(x, base=10) -> integer
> list(map(int, ["1", "2", "3"]))
[1, 2, 3]
Even if you define a function that takes two parameters, it complains about not having a second one: >>> def to_int(a, b):
... return int(a, base=b)
...
>>> list(map(to_int, ["1", "2", "3"]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: to_int() missing 1 required positional argument: 'b' def to_int(a, b):
...
with the JS semantic equivalent of def to_int(*args):
...
because that's how JS function declarations work. You simply have the option to assign names to positional parameters, i.e. function fn(a, b) {
comnsole.log(a, b)
}
is just syntactic sugar for function fn() {
const a = arguments[0]
const b = arguments[1]
console.log(a, b)
}and that's what people seem to struggle with and argument over for whatever strange reason.