Diamonds: cyclic inference error

Aleksey Shipilev aleksey.shipilev at
Mon Oct 15 07:21:41 PDT 2012

Hi guys, and Maurizio specifically :)

This is the test which fails to compile with current lambda/lambda:

------------- 8< -------------------------------------------------------

public class NoiseSampleTest {

    public void testL() {
        Map<String, Map<String, Counter>> map =
                new ComputeTreeMap<>(
			(s) -> new ComputeTreeMap<>(
				(x) -> new Counter()

        Assert.assertEquals(1, map.get("foo").get("bar").inc());
        Assert.assertEquals(2, map.get("foo").get("bar").inc());

    public static class ComputeTreeMap<K, V> extends TreeMap<K,V> {

        public ComputeTreeMap(Mapper<K, V> map) {
            // do nothing

        public V get(Object key) {
            throw new UnsupportedOperationException();

    public static class Counter {
        private int count = 0;
        public int inc() {
            return ++count;

-------------- 8< ----------------------------------------------------

javac says:
com/oracle/lambda/[16,16] error: cannot infer type
arguments for ComputeTreeMap<>
[ERROR]  cyclic inference - cannot infer target type for given
lambda/method reference expression

...and if I write out the explicit type arguments within the diamond,
the test compiles well. Is this a spec-ed behavior, or just a bug?


More information about the lambda-dev mailing list