Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reason: inferred type does not conform to upper bound(s)

I tried to look for similar answer but I didn't get/find a solution yet, then I ask directly exposing my case

I have a static function validate

    private static void validate(AiNode pNode) {
        ...
            for (AiNode child : pNode.mChildren) {
                doValidation(child.mMeshes, child.mMeshes.length, "a", "b");
            }
        }
    }

pNode.mChildren is an array of AiNode.

This is my doValidation

private static <T> void doValidation(T[] pArray, int size, String firstName, String secondName) {

        // validate all entries
        if (size > 0) {

            if (pArray == null) {

                throw new Error("aiScene." + firstName + " is NULL (aiScene." + secondName + " is " + size + ")");
            }
            for (int i = 0; i < size; i++) {

                if (pArray[i] != null) {

                    validate(parray[i]);
                }
            }
        }
    }

But I keep getting this error

method doValidation in class ValidateDataStructure cannot be applied to given types;
  required: T[],int,String,String
  found: int[],int,String,String
  reason: inferred type does not conform to upper bound(s)
    inferred: int
    upper bound(s): Object
  where T is a type-variable:
    T extends Object declared in method <T>doValidation(T[],int,String,String)
----
(Alt-Enter shows hints)

I think it has to do with the fact that arrays of primitive type in java extends Object[], indeed if I switch T[] to T it works, but then I cannot loop it anymore... I don't know how to solve it or which solutions applies best to my case.

The idea is to have different validate(T[] array) based on the array T type

like image 291
elect Avatar asked Sep 16 '15 08:09

elect


1 Answers

Your array is int[], not Integer[]. To convert it to Integer[] use

for (AiNode child : pNode.mChildren) {
    Integer[] meshes = Arrays.stream(child.mMeshes).boxed().toArray(Integer::new);
    doValidation(meshes, child.mMeshes.length, "a", "b");
}
like image 56
Tagir Valeev Avatar answered Nov 07 '22 15:11

Tagir Valeev