Skip to content
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

[Proposal] Static abstract members #12429

Closed
UnoSD opened this issue Jul 11, 2016 · 2 comments
Closed

[Proposal] Static abstract members #12429

UnoSD opened this issue Jul 11, 2016 · 2 comments

Comments

@UnoSD
Copy link

UnoSD commented Jul 11, 2016

Please consider adding the following language feature:

abstract class Base
{
    public static abstract string Something { get; }
}

class Derived1 : Base
{
    public static string Something => "Derived";
}

class Derived2 : Base { } // Compiler error.

Base.Something // Compiler error.

Derived.Something // OK.

void Something<T>() where T : Base { T.Something; // OK. }

A common pattern is to use Flyweight in this fashion:

class FlyweightFactory
{
    Dictionary<KeyType, Flyweight> _flyweights;
    public GetFlyweight(KeyType key) => _flyweights[key];
}

When it comes to populating the dictionary, in many cases the flyweight concrete classes have a unique key corresponding to their types. If we want to enforce the presence of the key on the sub types, we can define an abstract property/method on the base flyweight; but this means we have to create an instance only to get a constant value. On the other hand, we could use attributes (which are more fit for purpose), but there is no way to enforce an attribute.

The abstract static would solve this problem.

Or, as an alternative, an abstract attribute which must be overridden by the sub types.

@svick
Copy link
Contributor

svick commented Jul 11, 2016

This is very similar to another proposal, static interfaces: #2204.

@gafter
Copy link
Member

gafter commented Aug 10, 2017

Issue moved to dotnet/csharplang #809 via ZenHub

@gafter gafter closed this as completed Aug 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants