Java 8 Supplier with arguments in the constructor
But, a 1-arg constructor for T
that takes a String
is compatible with Function<String,T>
:
Function<String, Foo> fooSupplier = Foo::new;
Which constructor is selected is treated as an overload selection problem, based on the shape of the target type.
That's just a limitation of the method reference syntax -- that you can't pass in any of the arguments. It's just how the syntax works.
If you like method references so much, you can write a bind
method by yourself and use it:
public static <T, R> Supplier<R> bind(Function<T,R> fn, T val) {
return () -> fn.apply(val);
}
create(bind(Foo::new, "hello"));