Virtual/Abstract Static Members in Classes #9648
Replies: 3 comments 3 replies
-
Related to #9122 |
Beta Was this translation helpful? Give feedback.
-
Before there's any question of the automatic bubbling up of abstract static methods in class, the main thing is that the language does not yet allow abstract static methods in classes. This is mentioned in [Proposal]: Static abstract members in interfaces:
I skimmed all of https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/static-abstracts-in-interfaces.md#design-meetings for any discussion of this, but saw none. So, this may be the first request for static abstract/virtual members in classes. Randomly enough, I could have really used static abstract members in classes this past week. |
Beta Was this translation helpful? Give feedback.
-
Whilst I never requested this, I seem to stumble over this quite a lot. I used workaround 3 in two places, and at least in one place workaround 2. A quick search in our code base showed that there are currently 16 usages of abstract interface members, where this abstract base class issue is not hit. Unfortunately I can't use code search to find out, how often developers tried to use abstract interface members but upon failing resorted back to instance members. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In [Proposal]: Static abstract members in interfaces (#4436), it was suggested that a future feature may be adding support for static abstract members in classes:
There have been a few situations I have encountered where I run into a few conditions:
Take the following example:
This design does leave a few open questions:
Should we then allow the following?
base
keyword for static virtual members? I don't see any immediate restrictions but a static instance is not the same as a non-static instance, so there may be some interference.abstract
keyword on the class declaration required? I think it should be a requirement to avoid situations where an class containing a static abstract member becomes the most specific member, leaving the static abstract member undefined.Previous suggestion, bubbling up
With the new Static Abstract Interface Members feature, it would be really nice to be able to "bubble up" the requirement through an abstract base class to an implementing class.See the following simplified example:
I should be able to call
Giraffe.SpeciesName
and get "Giraffe", but another animal likeCat.SpeciesName
would return a "Feline".This echoes the existing behavior of if the
IAnimal
interface were applied directly to each animal type, but if you would like to provide some sort of common functionality (which arguably is the purpose of base classes), you run into a wall, namely CS0112 ("A static member cannot be marked as 'abstract'").Workarounds
There are a few workarounds currently:
AnimalService
, that provides the shared functionality and avoid the abstract base type entirely.static abstract
marker from theIAnimal
interface members and rely on instances.AnimalBase
I'm sure there's a very good reason this has not been done before now, even if that's just "nobody has championed a solution for this," but I'm definitely interested in hearing any flaws or drawbacks with my design.
Amendment: I think this should only be allowed in classes marked
abstract
. Non-abstract classes should probably not be allowed to bubble up members, but I'm interested in hearing counter-arguments.Beta Was this translation helpful? Give feedback.
All reactions