File: AmbiguousMethodReference.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (27 lines) | stat: -rw-r--r-- 692 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
The following code is fine in Java 7, but poses a problem in Java 8.

```java
static class A {
  B c(D d) { return null; }
  static B c(A a, D d) { return null; }
}
```

The method reference `A::c` of the instance method `c` has an implicit first
parameter for the `this` pointer. So both methods that `A::c` could resolve to
are compatible with `BiFunction<A, D, B>`, and the method reference is
ambiguous.

```java
void f(BiFunction<A, D, B> f) { ... }
```

```
error: incompatible types: invalid method reference
    f(A::c);
      ^
    reference to c is ambiguous
      both method c(A,D) in A and method c(D) in A match
```

Consider renaming one of the methods to avoid the ambiguity.