Generics shell is self-dependent

I came across the following:

I understand it:

In the class type parameters section, a variable of type T directly depends on a variable of type S if S is the boundary of T, and T depends on S if either T directly depends on S or T directly depends on a variable of type U which depends on S (using this definition is recursive).

But

This is a compile-time error if the type variable in the class type parameter section depends on itself.

what does it mean? Link

+6
source share
2 answers

What expression means means that a type parameter variable cannot depend on itself. The following code is not allowed:

class Generic<T extends T> { } 

Here T is a type parameter variable, and it cannot depend on itself (directly or invalidly). On the other hand, the following code is allowed:

 public class GenericRecursive<T extends GenericRecursive<T>> { } 
+6
source

This is a compile-time error if the type variable in the class type the parameter section depends on itself.

The parameter cannot be obtained by itself.

For example, this is not legal:

 class YourClass<S extends S> { } 

But you probably won’t use it as it makes no sense.

You can do this, for example:

 class YourClass<T extends S, S extends T> { } 

And it will not compile, because T depends on S , which depends on itself (cycle)

+2
source

Source: https://habr.com/ru/post/1014365/


All Articles