Hi Paul,
Very interesting problem!
I never assumed that inferring which method to choose does not depend on the context.
Of course it depends on the context, I agree with you. But that context is same as the compiler's context.
My point is that you don't need an
actual instance of an object to reference it as Class::instanceMethod.
However, the message in line //2 tells me that the compiler has resolved it to the instance method but doesn't accept it because there is no Tiger instance in that context.
The compiler tries its best to resolve method references: in this case, it could not find a parameter list that matches, namely (Animal a, Mouse m), so in this case it assumed that you wanted to use the instance method with only one parameter (Mouse). Obviously, that is not possible.
I'm starting to think that your referral to the "context that supplies an instance" is the same as my referral to "parameter lists must match"
If that is the case, I agree partially, as the context needs to supply a
reference to an object. Consider the code below. The context provides an implicit reference to an Animal by means of specifying BiConsumer<
Animal, Mouse>. However, the context
does need to provide an instance at the time you use the method!
Code: Select all
class Mouse { }
class Tiger{ }
class Animal {
public void eat(Mouse m){ }
public static void eat(Tiger t, Mouse m){ }
}
public class JavaApplication33 {
public static void main(String[] args) {
//The context does !not! supply any instance of Animal, but it compiles and it uses the instance method eat
BiConsumer<Animal, Mouse> bC = Animal::eat; //1
}
}
Kind regards,
Martijn