The root of the problem is versatility and verifiability, as well as design based on type constraints. The rule that we cannot overlap references (pointer) with type values is an implicit, multi-parameter constraint. So, we know that the CLR is smart enough to test this in non-general cases ... why not a generic one? That sounds attractive.
A correct definition of a general type is one that is subject to verification today for any type that exists (within the limits) and any that will be defined in the future. [1] CLR via C #, Richter. The compiler itself checks the definition of an open generic type, taking into account any type constraints that you specify in order to narrow down the possible type arguments.
In the absence of a more specific type restriction for Foo<T,U> T and U, each is a union of all possible values and reference types, and the interface is common to all these types (basic System.Object ), If we want to specify T or U , we can add restrictions of the primary and secondary types. In the latest C #, we can limit the class or interface to the most specific. struct or primitive type constraints are not supported.
Currently, we cannot say:
- where only
struct or value type - where T if T is a closed type
Example:
public struct TaggedUnion<T1, T2> where T1 : SealedThing
therefore, we have no way of defining a generic type that can be checked to never break the overlap rule for all types inside T and U Even if we could hold back the structure, you can still get a structure with reference fields, which for some type in the future, T<,> would be wrong.
So what we really ask here is why don't generic types allow implicit type constraints based on code within the class? ; An explicit layout is an internal implementation detail that imposes restrictions on which combinations of T1 and T2 are legal. In my opinion, this is not consistent with the design, depending on type restrictions. It violates the clean type-system contract developed. So why do you even have to face the problem of introducing a type restriction system in design in the first place, if we intend to break it down? We could also throw it away and replace it with exceptions.
With the current state of things:
- Type constraints are visible public type metadata of a generic type
- The general type check
Foo<T,U> is performed by the open definition of F<,> once. For each instance of the associated type, Foo<t1,u1> , t1 and u1 are checked for the correctness of the type with respect to the constraints. There is no need to re-read the code for the class and methods for Foo<t1,u1> .
All this "As far as I know"
There is no hard technical reason why each instance of a typical type cannot be semantically analyzed for correctness (this indicates C ++), but it seems to violate the design.
TL; DR
Without violating or supplementing the existing type of restriction, there is no possibility of verification.
Perhaps in combination with the corresponding new type restrictions, we can see this in the future.