Created
August 20, 2016 01:32
-
-
Save danieldietrich/18902076d53ef09b5c90472b613be85b to your computer and use it in GitHub Desktop.
JDK 1.8.0_101-b13 fails to infer types (partial example from Javaslang source)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HashMap<K, V> implements Map<K, V> { | |
public static <K, V> HashMap<K, V> ofEntries(Iterable<? extends Tuple2<? extends K, ? extends V>> entries) { | |
return ... | |
} | |
@Override | |
public HashMap<K, V> distinctBy(Comparator<? super Tuple2<K, V>> comparator) { | |
// IntelliJ IDEA shows no error, JDK fails to compile: | |
// "incompatible types: cannot infer type-variable(s) K,V,M" | |
return Maps.distinctBy(this, HashMap::createFromEntries, comparator); | |
} | |
private static <K, V> HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) { | |
return HashMap.ofEntries(tuples); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HashMap<K, V> implements Map<K, V> { | |
public static <K, V> HashMap<K, V> ofEntries(Iterable<? extends Tuple2<? extends K, ? extends V>> entries) { | |
return ... | |
} | |
@Override | |
public HashMap<K, V> distinctBy(Comparator<? super Tuple2<K, V>> comparator) { | |
// works fine with both, IntelliJ IDEA and JDK | |
return Maps.distinctBy(this, this::createFromEntries, comparator); | |
} | |
private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) { | |
return HashMap.ofEntries(tuples); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class Maps { | |
static <K, V, M extends Map<K, V>> M distinctBy(M map, OfEntries<K, V, M> ofEntries, | |
Comparator<? super Tuple2<K, V>> comparator) { | |
Objects.requireNonNull(comparator, "comparator is null"); | |
return ofEntries.apply(map.iterator().distinctBy(comparator)); | |
} | |
@FunctionalInterface | |
interface OfEntries<K, V, M extends Map<K, V>> extends Function<Iterable<Tuple2<K, V>>, M> { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment