Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't the Java 7 and Eclipse 3.8 compiler compile JDK code with the new Java 7 diamond operator?

import java.util.*;

public class SimpleArrays
{
  @SafeVarargs
  public static <T> List<T> asList( T... a )
  {
    return new ArrayList<>( a );
  }
}

asList() is taken from Oracles JDK implementation of java.util.Arrays.

The error is

error: cannot infer type arguments for ArrayList<>
    return new ArrayList<>( a );
1 error

How can this work? Oracle uses the same compiler that we do.

like image 898
Hannes Licht Avatar asked Aug 16 '11 12:08

Hannes Licht


1 Answers

Attention: The ArrayList used in the java.util.Arrays class is not java.util.ArrayList, but a nested class java.util.Arrays.ArrayList.

In particular, this class has an constructor which takes a T[] as argument, which java.util.ArrayList does not have.

Copy this class, too, and it will work.

like image 71
Paŭlo Ebermann Avatar answered Nov 15 '22 17:11

Paŭlo Ebermann