I much prefer languages like C#, C++ or TypeScript where the compiler warns me of such problems.
I much prefer languages like C#, C++ or TypeScript where the compiler warns me of such problems.
Though as I said in another comment, best-effort parsing was a decision that arose out of the Web ecosystem and its one of the few things about Javascript's language design that can't br blamed on incompetence.
"Variadic arguments" (really optional arguments with defaults/signature overloading, which is only arguably the same thing) existed long before JavaScript, and, indeed, the web. Optional arguments are also widely considered to be good/useful.
Honestly, most languages STILL won't catch this, because the typical pattern is to use method overloading to provide multiple signatures if default arguments aren't supported.
parseInt(in) parseInt(in, radix)
Any sane language having optional arguments is using keys for opt args. That's what common lisp and OCaml do.
In OCaml you would write:
let parse_int ?(radix = `Dec) string =...
val parse_int : ?radix:[`Bin | `Oct | `Dec | `Hex] -> string -> int
and call it like parse_int "42"
- 42
parse_int ~radix:`Bin "111"
- 7
List.mapi parse_int ["1"]
- Static error: this expression has type `string -> int`
but expected int -> 'a -> 'bNot saying one is better than the other, optional parameters can make for much less verbose code, I especially like parameter defaults introduced in ES6.
[1]: https://www.typescriptlang.org/play/#src=console.log(%5B'1'%...
parseInt is defined as accepting a string and an optional radix value, which is numeric. map is defined as providing the value and its index, which is also numeric. Would any of C#, C++, or TypeScript catch that without redefining either parseInt or map to require a more specific type, breaking compatibility with many millions of lines of code around the web?
C# would have a compile error with that map and parseInt definition because it can't coerce the types.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
The thing which would actually catch this would be the mismatch in the number of arguments (modulo someone declaring that third argument as optional) or breaking compatibility to change one of them not to be a basic integer.
You don't have to know if you don't use implicit type-casting. Make it explicit and you won't really have to worry about it.
"Explicit is better than implicit" is part of the Zen of Python for a very good reason.
Correct. Unfortunately a lot of people use implicit casting a lot and have no idea that that's what they are doing.
FTFY.
I know exactly how map works: https://en.m.wikipedia.org/wiki/Map_(higher-order_function)
This is map:
> a higher-order function that applies a given function to each element of a functor, e.g. a list, returning a list of results in the same order.
Since that is not what Array.prototype.map does, it is not map, it is some similar thing that is misnamed as map. If I wanted the index and the array as arguments, I would ask for them.
This reminds me of "array set" in Tcl (a language many probably haven't used in a long time -- or ever). Unlike the normal "set" command in Tcl, which overwrites the full value of the variable, "array set" is actually a merge operation -- it merges in new keys and doesn't get rid of anything. I've seen experienced programmers use "array set" in a loop, leading to awful bugs. This too could be dismissed by saying that they "don't even know how array set works," but actually I place the blame on the language for making it so easy to misuse.