method references: type inference, toString()

bitter_fox bitterfoxc at gmail.com
Wed Feb 22 07:26:09 PST 2012


2012/2/22 Paul Holser <pholser at gmail.com>

> Hi lambda-dev,
>
> 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);


Maybe in this case you don't need to convert by hand, if you help type
inference:
    assertThat(new BarredUpFoo(), PredicateMatcher.<Foo>matches(Foo::isBar);

My sample works well:

import java.util.functions.Predicate;

public class Test extends SuperTest
{
    interface Foo
    {
        boolean isBar();
    }

    public static <U> void create(Predicate<? super U> p) {}

    public static void main(String[] args)
    {
        //Test.create(Foo::isBar); // Error
        Test.<Foo>create(Foo::isBar);
    }
}

class SuperTest
{
    public static void create(Object o) {}
}

Regards,
bitter_fox


More information about the lambda-dev mailing list