7

While Java 8's type inference seems much improved, I've hit on a possible limitation and I'm not sure if there's some workaround I'm missing. The scenario:

class Foo<T> {
  <U> void apply(Function<T, Consumer<U>> bar) {}
}

class Bar {
  void setBar(String bar){}
}

Foo<Bar> foo = new Foo<>();

This works:

foo.<String>apply(bar -> bar::setBar);

This does not:

foo.apply(bar -> bar::setBar);

Is there any way to get type inference to work in this sort of situation?

Bobulous
  • 12,967
  • 4
  • 37
  • 68
Josh Stone
  • 4,328
  • 7
  • 29
  • 37
  • It's most certainly a bug rather than a limitation, because compiler does know everything it needs in both cases. But given Java history, I guess it will stay not fixed until Java 9 (in 5-10 years) – Display Name May 23 '14 at 22:27
  • I don't know if it's technically a bug - I had a hard time telling from the JLS if it should infer in this case or not. I'd love to know if there's something else I can do to workaround though. – Josh Stone May 23 '14 at 22:28
  • 2
    Are you using an IDE? Because it doesn't compile in Eclipse but does with javac for me. – Alexis C. May 23 '14 at 22:29
  • @ZouZou Great observation - I am using Eclipse! Very happy to know it works via JavaC, though publishing an API that Eclipse users can't use is a bummer. Guess I'll have to nag the Eclipse folks to fix things. – Josh Stone May 23 '14 at 22:31
  • For the workarounds — you can use Scala, it compiles similar code just fine: http://ideone.com/8GuI6n – Display Name May 23 '14 at 22:34

1 Answers1

6

It is an eclipse bug. Both compile fine with Netbeans or javac.

It seems that Eclipse has quite a few issues with java 8...

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783