views:

136

answers:

2

I was just looking at Guava's ImmutableList and I noticed that the of() method was overloaded 12 times.

It looks to me that all they needed was:

static <E> ImmutableList<E> of();
static <E> ImmutableList<E> of(E element); // not even necessary
static <E> ImmutableList<E> of(E... elements);

What's the reason for having so many similar variations?

A: 

Just like any overloaded function, it is overloaded to appropriately handle any possible applicable data type passed to it.

Steven
This doesn't have to do with data types, the overloads are purely for longer and longer lists of arguments of the same type.
ColinD
+10  A: 

Varargs and generics do not play nicely together. Varargs methods can cause a warning with generic arguments, and the overloads prevent that warning except in the rare case that you want to add more than 11 items to the immutable list using of().

The comments in the source say:

These go up to eleven. After that, you just get the varargs form, and whatever warnings might come along with it. :(

ColinD