views:

1168

answers:

6

I have defined a Java function:

static <T> List<T> createEmptyList() {
    return new ArrayList<T>();
}

One way to call it is like so:

List<Integer> myList = createEmptyList(); // Compiles

Why can't I call it by explicitly passing the generic type argument? :

Object myObject = createEmtpyList<Integer>(); // Doesn't compile. Why?

I get the error "Illegal start of expression" from the compiler.

A: 

It was quite some time since I dug into those parts of Java, but...

Why you can't do it was probably a design choice by the language developers. Still, due to the type erasure employed by Java, the generics information gets dropped at compile time anyway, so in your example it would create exactly the same byte code whether you had the type parameter or not.

Henrik Gustafsson
+9  A: 

You can, if you pass in the type as a method parameter.

static <T> List<T> createEmptyList( Class<T> type ) {
  return new ArrayList<T>();
}

@Test
public void createStringList() {
  List<String> stringList = createEmptyList( String.class );
}

Methods cannot be genericised in the same way that a type can, so the only option for a method with a dynamically-typed generic return type -- phew that's a mouthful :-) -- is to pass in the type as an argument.

For a truly excellent FAQ on Java generics, see Angelika Langer's generics FAQ.

.
.

Follow-up:

It wouldn't make sense in this context to use the array argument as in Collection.toArray( T[] ). The only reason an array is used there is because the same (pre-allocated) array is used to contain the results (if the array is large enough to fit them all in). This saves on allocating a new array at run-time all the time.

However, for the purposes of education, if you did want to use the array typing, the syntax is very similar:

static <T> List<T> createEmptyList( T[] array ) {
  return new ArrayList<T>();
}

@Test
public void testThing() {
  List<Integer> integerList = createEmptyList( new Integer[ 1 ] );
}
Cheekysoft
A: 

@Henrik Gustafsson

If I have a method like this:

static void someMethod(List<Integer> f) { .. }

I find that cannot call it like this:

someMethod(createEmptyList()); // Doesn't compile.  Again why?

So clearly I must have to specify the type somewhere to the compiler, and the syntax which doesn't work in the original question feels most natural.


I know I can work around this like so:

List<Integer> tmp = createEmptyList();
someMethod(tmp);

But this is cumbersome and will only look more ugly as I add more arguments to the method.

pauldoo
A: 

@Cheekysoft

That works a treat, but what kooky syntax!

pauldoo
A: 

@pauldoo Yes, you are quite right. It is one of the weaknesses with the java generics imho.

I response to Cheekysoft I'd like to propose to also look at how it is done by the Java people themselves, such as T[] AbstractCollection#toArray(T[] a). I think Cheekysofts version is superior, but the Java one has the advantage of familiarity.

Edit: Added link. Re-edit: Found a bug on SO :)


Follow-up on Cheekysoft: Well, as it is a list of some type that should be returned the corresponding example should look something like:

static <T> List<T> createEmptyList( List<T> a ) {
  return new ArrayList<T>();
}

But yes, passing the class object is clearly the better one. My only argument is that of familiarity, and in this exact instance it isn't worth much (in fact it is bad).

Henrik Gustafsson
A: 

When the java compiler cannot infer the parameter type by itself for a static method, you can always pass it using the full qualified method name: Class . < Type > method();

Object list = Collections.<String> emptyList();