4 ms·
Java knows at compile time exactly how many arguments are passed to a method, so presumably there's no adapter layer.
by PaulBGD_ 6y ago
Java knows at compile time exactly how many arguments are passed to a method, so presumably there's no adapter layer.
- The_rationalist 6y agoWrong, Java Varargs functions can take spreaded arrays as Var parameters hence the number for this not uncommon case (e.g jdbc apis, format(), etc) is only known at runtime
- colejohnson66 6y agoBut in Java (and similar), you have to be explicit about varargs. In JavaScript, every function supports varargs. This will run just fine: function print(arg) { console.log(arg); } print("a", "b", "c"); It’ll only output "a", obviously. In fact, this will run just fine too: print(); You’ll get "undefined" in the console, but it’ll work. JavaScript’s nature is that arguments are in the "arguments" pseudo-array, and the parameters are just fancy names for different values in "arguments". See: function count() { console.log(arguments.length); } count(); count("a"); count("a", "b"); In order, you’ll get: 0, 1, and 2. Despite the fact that "count" has no parameters in the definition. In the first function ("print"), "arg" is just syntax sugar for "arguments[0]". What I’m getting at is: in C, Java, etc., the compiler knows what is a varargs function and what isn’t. In JavaScript, the interpreter/compiler doesn’t and has to assume everything is.
- desert_boi 6y agohttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters#the_difference_between_rest_parameters_and_the_arguments_object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- colejohnson66 6y agoHence, “pseudo-array”
- The_rationalist 6y agoGreat explanation and I think you are right for Java, Kotlin, etc. But what about dynamic languages on the JVM such as GraalJS, groovy, etc?
- masklinn 6y ago> In the first function ("print"), "arg" is just syntax sugar for "arguments[0]". So much so that they reflect one another: you can set `arguments[0]` and retrieve that value from `arg`, and the other way around: function foo(arg0, arg1) { arg0 = "foo"; arguments[1] = "bar"; console.log(Array.from(arguments), arg0, arg1); } foo(1, 2) will log [ "foo", "bar" ] foo bar Although in reality optimising engines treat `arguments` as a keyword of sorts: they will not reify the object if they don't have to. However this meant they had to deoptimise a fair bit before the "spread" arguments arrived as that was how you'd call your "super".
- deleted 6y ago[deleted]
- bcrosby95 6y agoNote that there's no such thing as spreaded arrays in Java. Instead, varargs are just syntactic sugar for arrays.
- masklinn 6y ago> Java Varargs functions can take spreaded arrays as Var parameters That's syntactic sugar for an array, in the most literal way possible: when you define `func(Object… args)` the actual bytecode is that of `func(Object[] args)`, and you can pass in an array to stand in for the entire varargs. So no, they're right. The varags just counts as 1 formal parameter.
- jdmichal 6y agoYep! One can even define the `main` method using either: public static void main(String[] args) public static void main(String... args)