[Proposal]: Constrained interfaces #4066
Replies: 1 comment
-
I don't understand this suggestion. Both of your cases are already valid code today, because interfaces themselves are references types (which is achieved by boxing in the case of value types implementing interfaces) and because the |
Beta Was this translation helpful? Give feedback.
0 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.
-
FEATURE_NAME
Summary
Currently, there's no way of guaranteeing if an interface is a
class
or astruct
. I believe an interface should be able to specify if it must be a class or struct.Motivation
This would allow interfaces guaranteed to be references to be used as generics with reference constraints and for the
-nullable
flag to be used more effectively.Detailed design
An interface can "implement"
class
orstruct
same as with a generic constraint. Also similarly to the generic constraint,class
orstruct
must be first in the implementation list.Case 1: Nullable interface
Case 2: Used as generic with constraint
Drawbacks
Effectively "implementing"
struct
orclass
begins to defeat the original purpose ofinterfaces
(as opposed to C++ abstract classes). However, this rule has already been stretched for generic constraints.Alternatives
In some cases, an abstract class could be made as a loop whole to this. However then all the drawbacks of abstract classes apply.
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions