variadic-functions

Most specific method with matches of both fixed/variable arity (varargs)

风流意气都作罢 提交于 2019-11-27 19:27:33
问题 In section 15.12.2.5 of the Java Language Specification, it talks about how to choose the most specific method in both cases of methods with fixed arity and methods of variable arity (i.e. varargs ). What I can't find in the JLS is anything about deciding between two methods where one is of fixed arity and one of variable arity however. For example: public interface SomeApi { public String getSomething(String arg); // method 1 public String getSomething(String ... args); // method 2 }

Java generics and varargs

早过忘川 提交于 2019-11-27 18:45:27
I'd like to implement a function with both generics and varargs. public class Question { public static <A> void doNastyThingsToClasses(Class<A> parent, Class<? extends A>... classes) { /*** something here ***/ } public static class NotQuestion { } public static class SomeQuestion extends Question { } public static void main(String[] args) { doNastyThingsToClasses(Object.class, Question.class, SomeQuestion.class); // OK doNastyThingsToClasses(Question.class, SomeQuestion.class); // OK doNastyThingsToClasses(Question.class, Object.class, SomeQuestion.class); // compilation failure } } The

Java varargs method param list vs. array

耗尽温柔 提交于 2019-11-27 18:44:36
Varargs: public static void foo(String... string_array) { ... } versus Single array param: public static void bar(String[] string_array) { ... } Java 1.6 seems to accept/reject the following: String[] arr = {"abc", "def", "ghi"}; foo(arr); // accept bar(arr); // accept foo("abc", "def", "ghi"); // accept bar("abc", "def", "ghi"); // reject Assuming the above is true/correct, why not always use varargs instead of single array param? Seems to add a touch of caller flexiblity for free. Can an expert share the internal JVM difference, if there is one? Thanks. Arrays have been around from the

Repeated use of a variadic function argument doesn't work

落爺英雄遲暮 提交于 2019-11-27 17:45:13
问题 I have a function that tries to log stuff to the console and also to a log file, but it doesn't work. The second use of the variable length argument gives garbage written to the console. Any ideas? void logPrintf(const char *fmt, ...) { va_list ap; // log to logfile va_start(ap, fmt); logOpen; vfprintf(flog, fmt, ap); logClose; va_end(ap); va_list ap2; // log to console va_start(ap2, fmt); printf(fmt, ap2); va_end(ap2); } 回答1: The original code fails because it tries to use printf() where it

C++11: Number of Variadic Template Function Parameters?

白昼怎懂夜的黑 提交于 2019-11-27 17:39:51
How can I get a count of the number of arguments to a variadic template function? ie: template<typename... T> void f(const T&... t) { int n = number_of_args(t); ... } What is the best way to implement number_of_args in the above? Nawaz Just write this: const std::size_t n = sizeof...(T); //you may use `constexpr` instead of `const` Note that n is a constant expression (i.e known at compile-time), which means you may use it where constant expression is needed, such as: std::array<int, n> a; //array of n elements std::array<int, 2*n> b; //array of (2*n) elements auto middle = std::get<n/2>

Technically, how do variadic functions work? How does printf work?

China☆狼群 提交于 2019-11-27 17:29:42
I know I can use va_arg to write my own variadic functions, but how do variadic functions work under the hood, i.e. on the assembly instruction level? E.g., how is it possible that printf takes a variable number of arguments? * No rule without exception. There is no language C/C++, however, this question can be answered for both of them * Note: Answer originally given to How can printf function can take variable parameters in number while output them? , but it seems it did not apply to the questioner Sebastian Mach The C and C++ standard do not have any requirement on how it has to work. A

PHP Spread Syntax in Array Declaration

蹲街弑〆低调 提交于 2019-11-27 17:18:40
问题 PHP supports the spread syntax for variadic functions. In JavaScript, you can use the spread syntax to do this: var a = [1, 2]; var b = [...a, 3, 4]; console.log(b); // [1, 2, 3, 4] However, trying to do this in PHP: $a = [1, 2]; $b = [...$a, 3, 4]; var_dump($b);die; Results in this error: Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' Is using the spread syntax this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the same effect? 回答1: The

How does Haskell printf work?

蓝咒 提交于 2019-11-27 16:50:12
Haskell's type safety is second to none only to dependently-typed languages. But there is some deep magic going on with Text.Printf that seems rather type-wonky. > printf "%d\n" 3 3 > printf "%s %f %d" "foo" 3.3 3 foo 3.3 3 What is the deep magic behind this? How can the Text.Printf.printf function take variadic arguments like this? What is the general technique used to allow for variadic arguments in Haskell, and how does it work? (Side note: some type safety is apparently lost when using this technique.) > :t printf "%d\n" "foo" printf "%d\n" "foo" :: (PrintfType ([Char] -> t)) => t The

Java SafeVarargs annotation, does a standard or best practice exist?

坚强是说给别人听的谎言 提交于 2019-11-27 16:46:18
I've recently come across the java @SafeVarargs annotation. Googling for what makes a variadic function in Java unsafe left me rather confused (heap poisoning? erased types?), so I'd like to know a few things: What makes a variadic Java function unsafe in the @SafeVarargs sense (preferably explained in the form of an in-depth example)? Why is this annotation left to the discretion of the programmer? Isn't this something the compiler should be able to check? Is there some standard one must adhere to in order to ensure his function is indeed varags safe? If not, what are the best practices to

pass variable number of arguments in scala (2.8) case class to parent constructor

末鹿安然 提交于 2019-11-27 16:27:17
问题 I was experimenting with variable constructor arguments for case classes in Scala, but am unable to pass them to the constructor of a case classes' parent: abstract case class Node(val blocks: (Node => Option[Node])*) case class Root(val elementBlocks: (Node => Option[Node])*) extends Node(elementBlocks) the above doesn't compile... is it actually possible to do this? 回答1: This works with 2.7: abstract case class A(val a: String*) case class B(val b: String*) extends A(b:_*) Should work with