-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: RequiredMemberAttribute and NoRequiredMembersAttribute #64248
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsBackground and motivationAs part of supporting C# 11's Required Members feature, we need two new attributes:
API Proposalnamespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Field | AttributeTargets.Property)]
public sealed class RequiredMemberAttribute : Attribute { }
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class NoRequiredMembersAttribute : Attribute { }
} API Usage
A copy constructor for a type that has required members might look like this: public class C
{
public required int Property { get; init; }
public C() {}
[NoRequiredMembers]
public C(C c) => this.Property = c.Property;
}
// Usage
var c1 = new C { Property = 1 };
var c2 = new C(c1); // No error, constructor waives requirements Alternative DesignsI am open to alternative naming schemes. We had also considered a version of RisksNo response
|
@333fred, you didn't specify AllowMultiple or Inherited, which means these would be AllowMultiple=false and Inherited=true. Is that the intention? I assume "yes", just double checking. |
@333fred, I believe we discussed this, but for |
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Struct |
AttributeTargets.Field |
AttributeTargets.Property,
AllowMultiple=false,
Inherited=false)]
public sealed class RequiredMemberAttribute : Attribute
{
public RequiredMemberAttribute();
}
}
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Constructor,
AllowMultiple=false,
Inherited=false)]
public sealed class SetsRequiredMembersAttribute : Attribute
{
public SetsRequiredMembersAttribute();
}
} |
* Add attributes for required members Closes #64248. * Address PR feedback * Update src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems * Address PR feedback
Background and motivation
As part of supporting C# 11's Required Members feature, we need two new attributes:
RequiredMemberAttribute
andNoRequiredMembersAttribute
. They work as follows:RequiredMemberAttribute
will be applied to every type (class or struct) that defines required members, and to all the members (fields or properties) that are required by that type.NoRequiredMembersAttribute
will be applied to a constructor of a type that has required members, and then users of that constructor will not be required to set any members of the type when calling it. Think copy constructors for records: this is how they will communicate that calling the constructor does not require setting any members, because all members will have been set by the copy constructor itself.API Proposal
API Usage
RequiredMemberAttribute
will not be manually usable. It will be an error to apply it to C# code by hand.A copy constructor for a type that has required members might look like this:
Alternative Designs
I am open to alternative naming schemes. We had also considered a version of
RequiredMemberAttribute
where all the members were listed by name as a string argument to the constructor, but we redesigned after some community feedback and consultation with Wrighton to make sure that more attributes wouldn't break perf.Risks
No response
The text was updated successfully, but these errors were encountered: