When designing a (performant) method with variable parameter length like
<T> void consume(T t1)
<T> void consume(T t1, T t2)
<T> void consume(T t1, T t2, T t3)
and a) another overload with vararg at the end e.g. ImmutableList.of()
<T> void consume(T t1, T t2, T t3, T... others)
or b) another overload with single vararg parameter e.g. List.of()
<T> void consume(T... tees)
What are the advantages and disadvantages of each vararg overload?
One major distinction is whether you want to support passing a regular array without using varargs, e.g.:
String[] values = ...
List<String> list = List.of(values);
With Guava that wouldn't work, because there's no overload that just takes an array. Instead you would use the copyOf()
method that's tailor-made for arrays. Whether they created that method because of their of()
design or they limited of()
to distinguish it from copyOf()
is hard to say.