[JAVA] Variadic arguments in various languages

Many languages have the ability to pass any number of function arguments, but each has slightly different specifications.

C language

In C language, printf, which is used first in learning, is also composed of variable length arguments, but it is surprisingly difficult to create such a function.

Macros for variadic arguments are provided in a file called stdargs.h.

The characteristics of the implementation are as follows.

JavaScript

Up to ES5

In the case of JavaScript up to ES5, there is no special notation labeled as variable length argument, but it corresponds to the variable length argument itself because there is no problem even if the numbers do not match between the dummy argument and the actual argument. I can do it.

In a JavaScript function, the arguments are contained in an "array-like object" called ʻarguments, and you can get the number of arguments with ʻarguments.length. In addition, when there is a formal argument, there was a behavior that the element of ʻarguments` at the same position as the formal argument is linked, but it is abolished in the strict mode of ES5.

You can just turn the subscript with for, or if you want to convert it to a real array, you can do it with ʻArray.prototype.slice.call (arguments)`.

If you want to convert the contents of the array to an argument list, you can do it with function.apply (object to be this, array).

ES6 In ES6, it is possible to "receive a variable length argument as an array" and "expand an array etc. to the argument and pass it" with ... (spread operator) similar to PHP. Note that in ES6's Arrow Function (function notation using =>), ʻarguments` also ** inherits what is outside ** ([MDN](https://developer.mozilla. org / ja / docs / Web / JavaScript / Reference / arrow_functions)). Let's write it obediently with the spread operator.

Ruby Ruby explicitly supports variadic arguments, and takes the exact number of arguments. When declaring a formal argument, if there is an argument prefixed with *, it will receive the remaining arguments as an array (by nature, only one can be written in the argument list).

Number of arguments


def foo (arg1, *rest)
  #Requires one or more arguments
end

def bar (*args)
  #No problem
end

def baz (*args, last)
  #Separately*The attached argument does not have to be the last
end

If you want to expand the array and use it as an argument, you can expand it by writing like method (* arr).

PHP In the case of PHP, PHP 5.6 and later have the syntax ..., and like * in Ruby, it automatically packs variadic arguments into an array. You can also add type hints to this argument list.

Even in PHP 5.5 and earlier, the function can take more arguments than the formal arguments and can be processed by functions such as func_get_args and func_num_args.

If you want to expand an array as an argument, you can use ... in PHP 5.6 and later, and even before that you can use call_user_func_array.

Recommended Posts

Variadic arguments in various languages
Summary of root classes in various languages
[Swift] Variadic arguments
Parallel and parallel processing in various languages (Java edition)
Various pensions in Japan
Various threads in java
Use constructor with arguments in cucumber-picocontainer
How memory works in object-oriented languages
How to test private methods with arrays or variadic arguments in JUnit