Specific type member constraints for generic type arguments
Rekkonnect opened this issue · comments
Credits
Suggested by @ZacharyPatten
Summary
This feature is about having specific constraints about requiring certain members, including fields, properties, functions, events, and so on.
Functionality Design
The available constraint information would be:
- Member kind (required)
- Member name (required)
- Member accessibility (optional)
- Member return type (optional)
For properties or events:
- Accessors (optional)
For functions:
- Argument types (optional)
- Generic information (arity, constraints) (optional)
Such constraints can also be generalized on type constraint profiles.
API Design
Attributed Declaration
This feature introduces 6 new attributes that can be assigned to both interfaces (type constraint profiles) and directly on the type parameters themselves.
Each of those attributes declares that a required member of the specified member kind be declared with the aforementioned properties.
public class RequiredFieldTypeConstraintAttribute { }
public class RequiredPropertyTypeConstraintAttribute { }
public class RequiredEventTypeConstraintAttribute { }
public class RequiredMethodTypeConstraintAttribute { }
public class RequiredConstructorTypeConstraintAttribute { }
The only unsupported feature of attributed declaration is generic type constraint clauses. This is intentionally unsupported due to the complexity it adds to both the analyzer, and the end user that may wish to apply such constraints. Instead, templated declaration is preferred.
Templated Declaration
The aforementioned attributes may also be used in declaring the required member from an already explicitly declared member, acting as a template. In other words, a member found in a (preferably sealed) instance class type can act as a template for a required member declaration. Its accessibility, name, return type, etc. will all be taken into account. Instance class types are preferred for this case so that accessibility does not require an extra declaration attribute. An additional analyzer-internal identifier can be also declared on the member through the RequiredMemberTypeConstraintTemplateAttribute
attribute.