Java generics: What is the compiler's issue here? (“no unique maximal instance”)

前端 未结 4 1964
轻奢々
轻奢々 2021-02-20 17:41

I have the following methods:

public  T fromJson( Reader jsonData, Class clazz ) {
    return fromJson( jsonData, (Type)clazz );
}

public <         


        
4条回答
  •  失恋的感觉
    2021-02-20 18:39

    This seems like a failure of inference. The first method clearly intends to call the second method with the type argument being the same type parameter T that it has. But probably the compiler can't figure it out because its inference system is not good enough.

    In any case, you should be able to explicitly specify the type argument and it should get rid of the error:

    public  T fromJson( Reader jsonData, Class clazz ) {
        return this.fromJson( jsonData, (Type)clazz );
    }
    

提交回复
热议问题