News for hackers. Stuff that matters.
Re:Don't use functions as callbacks unless they're designed ... by franciscop on Saturday February 06, 2021 @05:30AM (#26045622)
I personally prefer extra arguments to just be ignored (and allow to use a default like `function fn(a, b='x') {`). I also believe in most other _dynamic_ languages this is allowed, so it's just about what kind of language it is, not just JS vs the rest.
Re:Don't use functions as callbacks unless they're designed ... by masklinn on Saturday February 06, 2021 @06:05AM (#26045770)
> 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

Re:Don't use functions as callbacks unless they're designed ... by franciscop on Saturday February 06, 2021 @06:23AM (#26045837)
This 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')
Re:Don't use functions as callbacks unless they're designed ... by username90 on Saturday February 06, 2021 @07:33AM (#26046110)
Yeah, 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.
Re:Don't use functions as callbacks unless they're designed ... by mechEpleb on Saturday February 06, 2021 @07:33AM (#26046112)
The 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.