4 ms·
> I also believe in most other _dynamic_ languages this is allowed Most definitely not. It's not allowed in Python, it's not allowed in Ruby, it's not allowed
by masklinn 6y ago
> I also believe in most other _dynamic_ languages this is allowed
Most definitely not. It's not allowed in Python, it's not allowed in Ruby, it's not allowed in any Lisp I know of[0], … it is allowed in PHP, which is about what I'd expect from that[1]. In most dynamic languages the arity is not a suggestion[2].
Which is exactly the issue at hand: `Array#map` was (stupidly) defined as calling its callback with 3 parameters. The last 2 are useless 99.99% of the time (and in better language you'd compose them in if and only if you needed them), as a result it's almost universal that you'd pass single-parameter callbacks which works… until it doesn't because the callback now takes 2+ parameters and starts taking in account the previously ignored garbage `Array#map` feeds it. The average JS developer likely doesn't even know Array#map callbacks receive 3 parameters, and usually aren't going to think about it: in 99% of cases it's has no relevance whatsoever.
[0] but most lisps make significant uses of variable-arity functions, which is a very different and much more formal proposition
[1] PHP's one saving grace being that HoFs have historically not been much of a thing, though I have not tracked how it's used these days
[2] as long as it's present at all AFAIK in Perl functions don't have formal parameters lists
- franciscop 6y agoThis seems to work on Python: def hello(a, b = 'world'): print(a, b) hello('hello') hello('hello', 'world') But these don't, so fair point: hello('hello', 'world', 'there) # nor def hello(a): ... hello('hello', 'world')
- username90 6y agoYeah, problem with JavaScript function signatures is that every function argument is optional, including all the arguments you didn't write. In python those functions would look like: def F(a = None, b = None, *args): ... And you can't write any other kind of function in javascript. I don't really like that aspect of javascript, it creates so many hard to debug situations.
- mechEpleb 6y agoThe first is an example of default argument syntax, it doesn't mean you can call functions with extra arguments, only that a value will be provided from the declaration if the call doesn't.