IssueRaiser

[Proposal]: Static abstract members in interfaces

# Static abstract members in interfaces * [x] Proposed * [ ] Prototype: Not Started * [ ] Implementation: Not Started * [ ] Specification: Not Started Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/static-abstracts-in-interfaces.md # Summary An interface is allowed to specify abstract static members that implementing classes and structs are then required to provide an explicit or implicit implementation of. The members can be accessed off of type parameters that are constrained by the interface. # Motivation [motivation]: #motivation There is currently no way to abstract over static members and write generalized code that applies across types that define those static members. This is particularly problematic for member kinds that *only* exist in a static form, notably operators. This feature allows generic algorithms over numeric types, represented by interface constraints that specify the presence of given operators. The algorithms can therefore be expressed in terms of such operators: ``` c# // Interface specifies static properties and operators interface IAddable<T> where T : IAddable<T> { static abstract T Zero { get; } static abstract T operator +(T t1, T t2); } // Classes and structs (including built-ins) can implement interface struct Int32 : …, IAddable<Int32> { static Int32 I.operator +(Int32 x, Int32 y) => x + y; // Explicit public static int Zero => 0; // Implicit } // Generic algorit

Posted by MadsTorgersen on Feb 12, 2021, 8:07 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Software)

[Proposal]: Static abstract members in interfaces

MadsTorgersenFeb 12, 2021, 8:07 PM

# Static abstract members in interfaces * [x] Proposed * [ ] Prototype: Not Started * [ ] Implementation: Not Started * [ ] Specification: Not Started Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/static-abstracts-in-interfaces.md # Summary An interface is allowed to specify abstract static members that implementing classes and structs are then required to provide an explicit or implicit implementation of. The members can be accessed off of type parameters that are constrained by the interface. # Motivation [motivation]: #motivation There is currently no way to abstract over static members and write generalized code that applies across types that define those static members. This is particularly problematic for member kinds that *only* exist in a static form, notably operators. This feature allows generic algorithms over numeric types, represented by interface constraints that specify the presence of given operators. The algorithms can therefore be expressed in terms of such operators: ``` c# // Interface specifies static properties and operators interface IAddable<T> where T : IAddable<T> { static abstract T Zero { get; } static abstract T operator +(T t1, T t2); } // Classes and structs (including built-ins) can implement interface struct Int32 : …, IAddable<Int32> { static Int32 I.operator +(Int32 x, Int32 y) => x + y; // Explicit public static int Zero => 0; // Implicit } // Generic algorit

Comments (0)

No comments yet. Start the discussion.

Comment on this issue