Type Witness in java generics

前端 未结 3 880
忘了有多久
忘了有多久 2020-12-31 17:11

I understand what Type Witness is as I see from Generics Trail In Java Documentation

BoxDemo.addBox(Integer.valueOf(10), listOfIntegerBoxes);
         


        
3条回答
  •  悲哀的现实
    2020-12-31 18:13

    For completeness, this was added in Java 5. Here are the relevant parts of JLS Third Edition, which covers Java 5 and 6:

    8.8.7.1 Explicit Constructor Invocations

    ExplicitConstructorInvocation:
        NonWildTypeArgumentsopt this ( ArgumentListopt ) ;
        NonWildTypeArgumentsopt super ( ArgumentListopt ) ;
        Primary. NonWildTypeArgumentsopt super ( ArgumentListopt ) ; 
    
    NonWildTypeArguments:
        < ReferenceTypeList >
    
    ReferenceTypeList: 
        ReferenceType
        ReferenceTypeList , ReferenceType
    

    15.12 Method Invocation Expressions

    MethodInvocation:
        MethodName ( ArgumentListopt )
        Primary . NonWildTypeArgumentsopt Identifier ( ArgumentListopt )
        super . NonWildTypeArgumentsopt Identifier ( ArgumentListopt )
        ClassName . super . NonWildTypeArgumentsopt Identifier ( ArgumentListopt )
        TypeName . NonWildTypeArguments Identifier ( ArgumentListopt )
    

    Note they are called NonWildTypeArguments. The term "Type Witness" does not appear in the JLS. In JLS SE 8, the invocation specs are rewritten to use the pre-existing notion of TypeArguments; and the word "witness" still appears nowhere.

    (MethodName already includes TypeName.Identifier, so that fifth method invocation defines the explicit usage of a Type Witness, which is why it is not marked optional.)

提交回复
热议问题