variadic-functions

When to prefer a varargs list to an array?

↘锁芯ラ 提交于 2019-12-29 07:33:07
问题 I'm implementing an API an have a method which you pass a list of paths where the program reads resources from public void importFrom(String... paths) { } I'm using varargs to make calling the method as convenient as possible to the user, like so obj.importFrom("/foo", "/foo/bar); Is this an appropriate use of varargs? Or is passing in an array better? 回答1: In your case varargs is just fine. You don't really need to make an array of the paths that you will be importing because there's nothing

Variable argument constructor _may_ conflict, but compiles

試著忘記壹切 提交于 2019-12-29 01:39:31
问题 I have two constructors that compile just fine but I'd expect Java to complain about the possibility of ambiguity. public Foo(int id, Bar bar, String name, String description){ } public Foo(int id, Bar bar, String... values){ } What gives? 回答1: Java allows these methods to exist, because it has rules about which one will be called if both apply. Specifically, the fixed arity method (without ... ) will be chosen over the variable arity method (with ... ). The JLS, Section 15.12.2, states the

Why overload the varargs method of() in Java Stream interface?

可紊 提交于 2019-12-29 00:08:16
问题 The Stream interface has two overloads for the method of() . One of these is a variable-arity method while the other takes a single argument. Is the single-argument method a performance optimization versus passing one argument to the variable-arity method? If so, how does it improve performance? The same questions could be asked of the empty() method, which would seem to be syntax sugar around the variable-arity of() . I see that the implementation differs between these methods, with the

Java unchecked: unchecked generic array creation for varargs parameter

佐手、 提交于 2019-12-28 09:34:45
问题 I have set Netbeans to show unchecked warnings in my Java code, but I am failing to understand the error on the following lines: private List<String> cocNumbers; private List<String> vatNumbers; private List<String> ibans; private List<String> banks; ... List<List<String>> combinations = Utils.createCombinations(cocNumbers, vatNumbers, ibans); Gives: [unchecked] unchecked generic array creation for varargs parameter of type List<String>[] Method source: /** * Returns a list of all possible

Difference between double… and double[] in formal parameter type declaration

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-28 06:51:12
问题 I have question: what is the difference between these two declarations? public static void printMax(double... numbers) { ... } public static void printmax(double numbers[]) { ... } Is double... numbers the same as double numbers[] ? 回答1: On varargs The Type... construct in method parameter declaration is commonly what is called varargs. In JLS, it's called the variable arity parameter. JLS 8.4.1 Format parameters The last formal parameter in a list is special; it may be a variable arity

java: how can i create a function that supports any number of parameters?

隐身守侯 提交于 2019-12-28 04:22:06
问题 is it possible to create a function in java that supports any number of parameters and then to be able to iterate through each of the parameter provided to the function ? thanks kfir 回答1: Java has had varargs since Java 1.5 (released September 2004). A simple example looks like this... public void func(String ... strings) { for (String s : strings) System.out.println(s); } Note that if you wanted to require that some minimal number of arguments has to be passed to a function, while still

Error with varargs for function-objects in Scala?

本秂侑毒 提交于 2019-12-28 03:04:25
问题 Why does this not work? val f = (args: Int*) => args.sum error: ')' expected but identifier found. val f = (args: Int*) => args.sum ^ This however works perfectly fine def sum(args: Int*) = args.sum val f = sum _ so does this val f: (Int*) => Int = args => args.sum btw. I'm using scala 2.9.1 回答1: I'm not an expert in specification reading, but it looks like the varargs Syntax is not supported for anonymous function. Compare the syntax for Function Declaration vs Anonymous Functions in the

difference fn(String… args) vs fn(String[] args)

荒凉一梦 提交于 2019-12-27 16:48:13
问题 Whats this syntax useful for : function(String... args) Is this same as writing function(String[] args) with difference only while invoking this method or is there any other feature involved with it ? 回答1: The only difference between the two is the way you call the function. With String var args you can omit the array creation. public static void main(String[] args) { callMe1(new String[] {"a", "b", "c"}); callMe2("a", "b", "c"); // You can also do this // callMe2(new String[] {"a", "b", "c"}

What is the point of overloaded Convenience Factory Methods for Collections in Java 9

旧街凉风 提交于 2019-12-27 12:01:06
问题 Java 9 comes with convenience factory methods for creating immutable lists. Finally a list creation is as simple as: List<String> list = List.of("foo", "bar"); But there are 12 overloaded versions of this method, 11 with 0 to 10 elements, and one with var args. static <E> List<E> of(E... elements) Same is the case with Set and Map . Since there is a var args method, what is the point of having extra 11 methods? What I think is that var-args create an array, so the other 11 methods can skip

How to create a polyvariadic haskell function?

妖精的绣舞 提交于 2019-12-27 11:08:27
问题 I need a function which takes an arbitrary number of arguments (All of the same type), does something with them and afterwards gives a result back. A list of arguments is impracticable in my specific case. As I looked through the haskell libs, I saw that the function printf (from module Text.Printf ) uses a similar trick. Unfortunately, I couldn't understand that magic by looking at the source. Can somebody explain how to achieve this, or at least some webpage/paper/whatever where I could