News for hackers. Stuff that matters.
Re:Don't use functions as callbacks unless they're designed ... by dsego on Saturday February 06, 2021 @05:43AM (#26045673)
How would a type check prevent this?
Re:Don't use functions as callbacks unless they're designed ... by magicalhippo on Saturday February 06, 2021 @06:03AM (#26045760)
Well the issue mentioned is that map calls the callback function with up to three arguments[1].

A type check could prevent this because it would require map to take a reference to a function with three parameters, or the compiler would complain inside the map implementation.

Similarly, passing it a function with only one parameter would be a type violation and the compiler would complain.

Now in a language with type checking, you could still potentially run afoul.

Say the map function was overloaded with one variant for one-parameter callbacks, one variant for two-parameter callbacks etc. Then the compiler might figure out it could use the second overload if the "toReadableNumber" function got changed to take the extra "base" parameter.

So again you end up with the numbers getting converted with a variable base.

Though, IMHO, having such an overloaded map function is inviting trouble and is a very poor design.

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:Don't use functions as callbacks unless they're designed ... by mr_toad on Saturday February 06, 2021 @07:48PM (#26051616)
> or the compiler

Functions can be assigned at runtime. It would have to be a runtime error.

Re:Don't use functions as callbacks unless they're designed ... by dirkt on Sunday February 07, 2021 @03:06AM (#26053466)
A proper typesystem for functional types will check at compile-time that in all places where you would assign such a function, the function will always have the right type.

So it's NOT a runtime error, it's a compile-time error, even though you can assign different functions at runtime.

Re:Don't use functions as callbacks unless they're designed ... by mr_toad on Sunday February 07, 2021 @09:04PM (#26060045)
You can assign a reference to a function and pass that to a function that expects a callback, and then change that assignment based on incoming data. And if that’s not enough you can create and modify functions at runtime. And you’d have to content with JavaScript’s spread syntax and rest parameters.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:Don't use functions as callbacks unless they're designed ... by magicalhippo on Monday February 08, 2021 @07:02AM (#26063386)
But it would only have to be a runtime error (your words) for the code which is dynamically loaded.

Static code will get checked at compile-time, even though the exact function passed as callback depends on the incoming data via a series of if/case statements say, from a dictionary or whatever.

Re:Don't use functions as callbacks unless they're designed ... by btinker on Saturday February 06, 2021 @05:58AM (#26045742)
It would check the arguments and return type of the function. Map takes a function with three arguments, toReadableNumber only takes one, therefore the functions are of a different type. So someNumbers.map(toReadableNumber) would be an error and not execute at all, instead of being a "bad practice" / potential mistake.
Re:Don't use functions as callbacks unless they're designed ... by dsego on Saturday February 06, 2021 @08:30AM (#26046358)
What if toReadable also takes 3 args of the same type but different meaning?
Re:Don't use functions as callbacks unless they're designed ... by flohofwoe on Saturday February 06, 2021 @09:18AM (#26046626)
You can create custom types to communicate semantics to the type checker. Whether that's always a good idea is arguable though, but the tools are there (e.g. there's a wide field between weak and strong typing, and an overly strong type checker can be quite a hassle to work with while an overly weak type checker isn't much better than duck typing).
Re:Don't use functions as callbacks unless they're designed ... by choeger on Saturday February 06, 2021 @05:50AM (#26045705)
A type check would give map a type, say [a] -> (int -> a -> b) -> [b]. If you try to apply it to any function that does not have the type (int -> a -> b), the check would fail.
Re:Don't use functions as callbacks unless they're designed ... by username90 on Saturday February 06, 2021 @05:57AM (#26045732)
So basically any language that does any form of type checking at all.
Re:Don't use functions as callbacks unless they're designed ... by wruza on Saturday February 06, 2021 @06:27AM (#26045857)
See also magicalhippo’s comment on overloading. We could introduce real types instead of primitives, so that ‘number_formatting_base_t’ would conflict with ‘array_index_t’, but nobody in the world bothers beyond bare ‘int’.
Re:Don't use functions as callbacks unless they're designed ... by username90 on Saturday February 06, 2021 @07:24AM (#26046083)
Yeah, I see the point, in for example C# if you have overloaded methods it tries it best to map it to the accepted type. However you'd have to work really hard to invent a case where a library updates a signature to accept another type, and then you in your code have 2 overloads of a method that you pass and it now choses the wrong version, all while the library change doesn't break any other code.

Scenario: Library changes signature so you always pass functions with more parameters.

Result: This will break almost every codebase, they probably wouldn't do this in a minor patch.

Scenario: Library adds a new signature where you can pass functions with more parameters and keep them overloaded between each other.

Result: Compiler can't identify which of the two signatures to pass your overloaded function and throws a compilation error.

Re:Don't use functions as callbacks unless they're designed ... by kuschku on Saturday February 06, 2021 @07:15AM (#26046046)
Actually, that's what I use inline types in kotlin for.

So username, password are distinct from string and objectid might be backed by an int, but is distinct from int.

Obviously, once compiled, there's no overhead, it's a zero cost abstraction. But an incredibly useful one.

Re:Don't use functions as callbacks unless they're designed ... by Akronymus on Saturday February 06, 2021 @06:30PM (#26051154)
On my personal projects with F#, never got a chance to use it professionally, I actually use them. Makes modeling the data much easier.
Re:Don't use functions as callbacks unless they're designed ... by MaxBarraclough on Saturday February 06, 2021 @06:15AM (#26045799)
Not including TypeScript.
Re:Don't use functions as callbacks unless they're designed ... by xsmasher on Saturday February 06, 2021 @09:03PM (#26051962)
Just to clarify, if you update toReadableNumber to take a second parameter and that parameter is not a number, typescript will complain.*

TypeScript won't catch the original landmine because it ignores the extra parameters; maybe some linter would? Is there a rule that enforces "functions used by map must spell out all the parameters?"

* Example of typescript giving an error on toReadableNumber_v2: https://www.typescriptlang.org/play?#code/FAYw9gdgzgLgBAQwE5...