Java 8 and Generalized Target-Type Inference

后端 未结 2 2053
慢半拍i
慢半拍i 2020-12-10 01:29

I have installed the last JDK 8 ea b114 to test the new language features. However the inference in chained calls seems not to work yet.

If I write:

         


        
2条回答
  •  不知归路
    2020-12-10 02:18

    As @Holger said, Java 8 improved contextual inference so that this works_

    public static  Iterator iter(Iterable i)
    {
        return i.iterator();
    }
    
    public static void main(String[] args)
    {
        Iterator it = iter( new ArrayList<>() );
                    \____________________________/
    }
    

    It didn't work in Java 7 — the inference on new ArrayList<>() could not depend on context.


    It'll be a huge change to the language to do what you want in the question. John Rose started a similar discussion, see http://mail.openjdk.java.net/pipermail/lambda-dev/2013-July/010531.html


    Too much inference and too much contextual dependency can be a bad thing. It's not so much that the compiler cannot handle the complexity - it can. It's about whether human programmers can handle it. I am sensing that Java 8 is already at a dangerous level that the codes will be difficult for humans to parse.

提交回复
热议问题