Lessen Generic Type Parameter Substitution Restriction when Implementing same Interface for different T
s
#9248
Unanswered
fitdev
asked this question in
Language Ideas
Replies: 1 comment 1 reply
-
The check done in CLR doesn't seem to take generic constraint into account: However, there's other comment hinting that some check can be delayed at instantiation time: Despite that, multiple implementation of generic interface is already distorted. Consider this example: var c = new C();
Console.WriteLine((c as I<Base>).Dispatch());
class C : I<Derived1>, I<Derived2>
{
string I<Derived1>.Dispatch() => "Dispatched with Derived1";
string I<Derived2>.Dispatch() => "Dispatched with Derived2";
}
class Base { }
class Derived1 : Base { }
class Derived2 : Base { }
interface I<out T>
{
string Dispatch();
} The covariant dispatch result will depend on the declaration order of interface implementation. I don't think it's encouraged to support more corner cases for this. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Considering code like:
It seems that CS0695 is too restrictive:
T
is constrained toclass
therefore it can never bebool
, and thereforebool
andT
may never in fact unify. Hence it should be allowed for a type to implement both generic versions of the same interface for different Ts.Ideally this restriction should be lessened even more to cover various reference type hierarchies where it can be shown that again Ts won;t unify (due to having different inheritance hierarchies).
Beta Was this translation helpful? Give feedback.
All reactions