3 ms·
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
by magicalhippo 6y ago
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/Reference/Global_Objects/Array/map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- mr_toad 6y ago> or the compiler Functions can be assigned at runtime. It would have to be a runtime error.
- dirkt 6y agoA 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.
- mr_toad 6y agoYou 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/Reference/Global_Objects/Function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- magicalhippo 6y agoBut 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.