What does the three dots in the parameter list of a function mean?

These type of functions are called variadic functions (Wikipedia link). They use ellipses (i.e., three dots) to indicate that there is a variable number of arguments that the function can process. One place you’ve probably used such functions (perhaps without realising) is with the various printf functions, for example (from the ISO standard): int printf(const … Read more

Why does type-promotion take precedence over varargs for overloaded methods

JLS 15.12.2 is the relevant bit of the spec to look at here. In particular – emphasis mine: The remainder of the process is split into three phases, to ensure compatibility with versions of the Java programming language prior to Java SE 5.0. The phases are: The first phase (ยง15.12.2.2) performs overload resolution without permitting … Read more

How to work with varargs and reflection

Test.class.getDeclaredMethod(“foo”, String[].class); works. The problem is that getMethod(..) only searches the public methods. From the javadoc: Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. Update: After successfully getting the method, you can invoke it using: m.invoke(this, new Object[] {new String[] {“a”, “s”, … Read more

How do you call an Objective-C variadic method from Swift?

Write a va_list version of your variadic method; + (NSError *)executeUpdateQuery:(NSString *)query, … { va_list argp; va_start(argp, query); NSError *error = [MyClassName executeUpdateQuery: query args:argp]; va_end(argp); return error; } + (NSError *)executeUpdateQuery:(NSString *)query args:(va_list)args { NSLogv(query,args); return nil; } This can then be called from Swift MyClassName.executeUpdateQuery(“query %d, %d %d”, args: getVaList([1,2,3,4])) Add an extension … Read more