satnam
satnam

Reputation: 11474

Did I find a compiler bug in java 9?

I'm getting a compile error in Eclipse on line 11.

Type mismatch: cannot convert from List<Object> to List<JavaCompilerBug.Foo>

I believe this code should compile and I've created a short example that demonstrates the problem:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class JavaCompilerBug {

  public static void main(String[] args) {
    List<String> vals = Arrays.asList("1", "2", "3");

    List<Foo> foos = map(vals, s -> Foo.with(last(vals)));
  }

  public static class Foo {
    public static Foo with(String value) {
      return new Foo();
    }

    public static Foo with(Foo value) {
      return new Foo();
    }
  }

  public static <A, B> List<B> map(List<A> input, Function<A, B> function) {
    List<B> ret = new ArrayList<>();
    for (A element : input) {
      ret.add(function.apply(element));
    }
    return ret;
  }

  public static <T> T last(List<T> c) {
    return c.get(c.size() - 1);
  }

}

Upvotes: 0

Views: 181

Answers (1)

satnam
satnam

Reputation: 11474

The problem is actually with Eclipse and not the Java compiler. This issue has been filed with Eclipse here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=532860

Upvotes: 4

Related Questions