I am using java 8 and I would like to detect subtle class differences at compile time modifying withProperty()
header. This code is working but I would like to force a compilation error in main()
function because this::getInteger
returns an Integer and the second argument is a String.
import java.util.function.Function;
public class MatcherProperty<T, K> {
public static <T, K> MatcherProperty<T, K> withProperty(
Function<T, K> mapper,
K expected
){
return new MatcherProperty<>();
}
private Integer getInteger(Object object) {
return 1;
}
public void main() {
withProperty(this::getInteger, "Random string");
}
}
I would like to avoid (if possible) a third argument in withProperty()
function specifying the class type or something like this. Maybe K is translated to Object, the superclass of Integer an String. What is actually happening under the hoods? Is it possible to force a compilation error in this case?
Thanks in advance.