method references: type inference, toString()

Brian Goetz brian.goetz at oracle.com
Tue Feb 21 15:07:38 PST 2012


> 1) Should the usage of Foo::isBar as the predicate fed to
> PredicateMatcher.matches() compile successfully? I'm hoping not to
> have to perform the SAM conversion by hand:
>
>      Predicate<Foo>  bar = Foo::isBar;
>      assertThat(new BarredUpFoo(), PredicateMatcher.matches(bar);

Without answering the question on whether the inference should succeed 
or not (the exact details of type inference are currently in flux), I'll 
point out that there is a less intrusive form of "by hand" that can be 
used to provide the needed additional type info to the compiler -- use a 
cast to provide the target type.  This is less intrusive that having to 
name a local variable.

   assertThat(new BarredUpFoo(),
              PredicateMatcher.matches((Predicate<Foo>) Foo::isBar));

> 2) Wondering if it wouldn't be too much trouble to have method
> references respond to toString() with something like "Foo::isBar" or
> "method isBar on class Foo", instead of, e.g. "Main$1 at a30a4e". It'd
> make the output of PredicateMatcher.matches() really slick.

Yes, the details of this are being actively discussed in the EG, and is 
a desirable goal.  Lots of details to work out, of course.



More information about the lambda-dev mailing list