Proposal: Allow use of static methods/properties in Interfaces #1353
Replies: 6 comments
-
[Proposal] Static abstract members: #809 Duplicate of dotnet/roslyn#2204. |
Beta Was this translation helpful? Give feedback.
-
Glad to see I'm not the only who wants this. :) |
Beta Was this translation helpful? Give feedback.
-
Check out #164, it's aim is to solve a lot of these problems through some compiler sorcery. Actually adding required static members to interfaces would be impossible. Virtual dispatch requires a virtual table which belongs to a specific instance. |
Beta Was this translation helpful? Give feedback.
-
Oh, only non-virtual static methods will be available for interfaces in #52. My mistake. @HaloFour has a good point. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour What do you mean each instance holds a v-table? An object would need know only about its particular implementation right? I thought a v-table was a dictionary that lived in a class definition. |
Beta Was this translation helpful? Give feedback.
-
Yes, there is only one vtable per class definition, but each instance has its own pointer to that specific vtable for its type. Virtual dispatch has to dereference that pointer to get to the vtable to know which method to call. Without an instance there wouldn't be a pointer to a vtable and the CLR wouldn't know where to dispatch that call. |
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.
-
Scenario: I have an
INumeric
interface. I want implementations ofINumeric
to have a staticParse
method and a staticZero
property.What I would like:
The proposed change is simply:
T.Zero
)abstract static
declarations with similar behaviorAs things sit, I have to use hacky workarounds to get generic code working, or implement what should be static methods as instance methods.
Beta Was this translation helpful? Give feedback.
All reactions