[Proposal] Allow overriding single accessor properties with multiple accessors #6291
Replies: 17 comments 5 replies
-
The accessor methods are considered separate, but the property metadata isn't. If a type tries to declare an overridden property and point an accessor to a method in the base class that type will not verify. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Am I missing something here? The newly property is adding an accessor, not removing one, so there won't be any case of a derived property pointing to an accessor in the base class. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour You don't have to point it to a method in the base class, you point it to the method that's in the derived class. That's already what happens when you In other words, this code: public abstract class Base
{
public abstract string Prop { get; }
}
public class Derived : Base
{
public override string Prop
{
get => null;
set {}
}
} would produce the following IL, which peverifies fine.
|
Beta Was this translation helpful? Give feedback.
-
Yeah, I think I read the proposal backwards, where you're looking to override a single accessor from a base class that declares both as |
Beta Was this translation helpful? Give feedback.
-
As I've posited in the related thread, an alternative syntax might be to require |
Beta Was this translation helpful? Give feedback.
-
@Joe4evr That would give us a different syntax between interfaces and classes when implementing/overriding a single-accessor property with a two-accessors property. Furthermore, currently |
Beta Was this translation helpful? Give feedback.
-
I came across this restriction many times, please consider its implementation and inclusion in the language. |
Beta Was this translation helpful? Give feedback.
-
I would like to have this feature too Also is it possible to separately define abstractness of class A
{
public int X { get => x; virtual set => x = value; } // cannot override get
public bool Condition { abstract get => x; } // may or may not implement set
public abstract bool Condition { get => x; } // cannot implement set, current behaviour
} |
Beta Was this translation helpful? Give feedback.
-
Have needed this a couple of times. Usually ended up writing the getter in the base class and throwing notsupportexceptions. But that doesn't give you much compiler/design time validation/security/error checking. |
Beta Was this translation helpful? Give feedback.
-
This should also apply when the overridden property has a covariant type, as discussed in #5179. In that case, the ability to add a setter in the child class may be especially important, since there's no way to define even a dummy setter in the parent class (that just throws) without the compiler complaining about a legitimate type safety issue. |
Beta Was this translation helpful? Give feedback.
-
I've also been burned by this restriction, and would love to see the suggested fixes implemented. |
Beta Was this translation helpful? Give feedback.
-
It's surprising that with this many duplicates and upvotes no official has responded yet. I would like to know if there is a serious problem with it. |
Beta Was this translation helpful? Give feedback.
-
I'd love to have this too. |
Beta Was this translation helpful? Give feedback.
-
Agreed. Can we get some LDT attention on this? |
Beta Was this translation helpful? Give feedback.
-
I periodically stumble in this limitation as well. It would be very interesting to take care of it as well after finally covariant return types were allowed in overrides. |
Beta Was this translation helpful? Give feedback.
-
So the corresponding issue is closed as it's moved to a discussion, duplicates get closed too - but still no answer here - any update? |
Beta Was this translation helpful? Give feedback.
-
I believe the community is still waiting for this one? It's rather confusing that it works for interfaces but not for classes. |
Beta Was this translation helpful? Give feedback.
-
Copied and improved from the original issue dotnet/roslyn#9482
Description
Currently, a
virtual
orabstract
property having a singleget
orset
accessor can't be overridden with a property having both accessors. This proposal removes this limitation.How the override is written doesn't matter: explicit accessors as well as auto-properties are permitted.
The following code is now valid:
Why
private protected
).Of course I could have added a SetWidth() method, but this didn't feel right.
(Note: as with any code problem, this can be - and has been - solved differently: let's not discuss that, it's just a sample.)
Impact
CLR
The CLR already allows this. As far as it's concerned, the two accessors are separate methods, and the override/abstract/virtual modifiers are applied on each method, not on the property itself.
I've compiled a code equivalent to the sample above using ilasm, verified that it's valid according to peverify, and ran it successfully.
Compiler
The following compiler errors are removed:
I've already made some changes in a private branch, those errors are pretty straightforward to remove. No changes are needed in code generation. I'll add proper tests and send a pull request if this proposal is accepted.
Compatibility
The current compiler and tooling can already handle assemblies using those overrides.
Since it's always been a valid construct at the metadata level, I suspect pre-Roslyn versions of the compiler have no problem handling this, but I've not tested them.
Implicit implications
The newly added accessor is automatically marked as
virtual
, unless the property or its containing class issealed
. This seems like a sensible choice, sinceoverride
already impliesvirtual
.Spec change
In the classes specification, the following text:
is replaced by:
Beta Was this translation helpful? Give feedback.
All reactions