Allow interfaces with static abstract methods as type arguments when usage is safe #9668
Unanswered
DPDmancul
asked this question in
Language Ideas
Replies: 2 comments 4 replies
-
How does the compiler know this? In metadata the signatures are the same. |
Beta Was this translation helpful? Give feedback.
1 reply
-
See #5955. |
Beta Was this translation helpful? Give feedback.
3 replies
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.
-
Currently, interfaces with
static abstract
members are treated as completely prohibited in type argument positions. The compiler disallows using them as type parameters, even in contexts where it would be perfectly safe.The reason for the prohibition is clear: if a type parameter is constrained to such an interface, the caller could invoke the static abstract member, which is undefined (just like instantiating an abstract class and calling an abstract method).
But consider the following example
Problematic<IMyInterface>()
should remain invalid, because it can invoke an unimplemented static abstract method;Safe<IMyInterface>()
should be allowed, since no static abstract member access is possible.Indeed we can declare arrays with these interfaces as element types (i.e
IMyInterface[]
). For the same reason we should (but currently cannot) be allowed to declare lists, enumerables,... and also to call extension methods on them. Currently you can declare an array of interfaces with static abstract methods, but you cannot, for example, use LINQ on them, since even if the type parameter of the extension methods in inferred, it still cannot be set as such interfaces. But there is no reason for this: in such cases where there is no harm (since you cannot call the abstract methods, not knowing them), it should'nt be disallowed to use such interfaces as type parameters.Proposal
Relax the restriction so that:
Beta Was this translation helpful? Give feedback.
All reactions