-
Notifications
You must be signed in to change notification settings - Fork 6k
Provide "static virtual interface members" with examples and description of static virtual
methods
#43130
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
base: main
Are you sure you want to change the base?
Provide "static virtual interface members" with examples and description of static virtual
methods
#43130
Conversation
docs/csharp/whats-new/tutorials/snippets/staticinterfaces/GetDuplicated.cs
Outdated
Show resolved
Hide resolved
@BartoszKlonowski Do you want to come back to this one? I think it's close. |
@BillWagner Yes, let me come back to this considering the last input you made here 👍 |
@BartoszKlonowski Thanks. Also, as it's now mid-August, I have less concerns about using the |
dd40445
to
6929ea6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this @BartoszKlonowski
I had one wording suggestion. Let me know what you think, and then I'll merge this.
@@ -66,6 +66,26 @@ AAAAAAAAAA | |||
|
|||
This small example demonstrates the motivation for this feature. You can use natural syntax for operators, constant values, and other static operations. You can explore these techniques when you create multiple types that rely on static members, including overloaded operators. Define the interfaces that match your types' capabilities and then declare those types' support for the new interface. | |||
|
|||
## Static virtual interface methods | |||
|
|||
Similar can be achieved using the static virtual methods of an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated<T>` interface, providing any type with a constraint that a type will implement the `operator ++`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This opening sentence seemed out of place somehow. How about this alternative:
Similar can be achieved using the static virtual methods of an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated<T>` interface, providing any type with a constraint that a type will implement the `operator ++`: | |
You can also provide a default implementation for static methods defined in an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated<T>` interface, providing any type with a constraint that a type will implement the `operator ++`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this.
There's one build error that we need to fix. I have a suggestion that emphasizes the fact that default interface members must be accessed through a reference of the interface type.
LMK what you think, and then I'll merge.
@@ -9,6 +9,11 @@ | |||
Console.WriteLine(str++); | |||
// </TestRepeat> | |||
|
|||
// <TestUniqueInstance> | |||
var instance = UniqueInstance.Instance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this to work, you'll need to use the interface type:
var instance = UniqueInstance.Instance; | |
var instance = (UniqueInstance as ISingleton)?.Instance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner I committed this suggestion, although locally, with VS still on version 17.4 I do have issues with your suggestion: CS0119 for UniqueInstance and CS0305 for ISingleton (which can be fixed by specifying the T type for this cast).
Let's see if remotely it will also fail and then we can check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BillWagner I should mention, that locally, with VS v17.4 I can also see the initial error for Instance
not being a part of UniqueInstance
.
Somewhere I read that static virtual
feature requires:
- .NET v8.0 or above - locally I'm running on .NET 9, and I see this
staticinterfaces
project runs on .NET 8, which should be fine? - preview enabled as
LangVersion
- we have that. - VS 17.6 or above - this is what I'm missing locally, but can't say regarding the CI checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the new errors. To answer your question:
.NET v8.0 or above - locally I'm running on .NET 9, and I see this staticinterfaces project runs on .NET 8, which should be fine?
Yes, it should be. As a normal practice, we're happy to move to the latest GA release (.NET 9 now).
preview enabled as LangVersion - we have that.
Check.
VS 17.6 or above - this is what I'm missing locally, but can't say regarding the CI checks.
We should be using the .NET 10 preview build (because some of the docs are for the preview).
Now, this example might be tricky to get right unless the type overrides the interface method (even if it just calls the interface implementation.) Why? Well, an ISingleton<int>
could be implemented by more than one class type. So, which one should be used? Without an instance of the type, that's hard to express.
Co-authored-by: Bill Wagner <[email protected]>
This pull request closes #41668
It adds the section presenting the static virtual methods of an interface.
This PR adds the separate implementation files for
IGetDuplicated
interface containing thestatic virtual
example as well as theDuplicate
type implementing that interface.The implementation of the
Duplicate
struct is (on purpose) very similar to already existingRepeatSequence
type, so that it bases on the reader's understanding and provides them with the comparison betweenstatic abstract
andstatic virtual
scenarios.The description is brief, with only required details, and follows the same style/layout as the
## Static abstract interface methods
section.The only addition is the note regarding the pitfall of leaving the static virtual method unimplemented. Compiler won't scream about lack of it explicitly, so I wanted to highlight that having the default implementation does not mean that this default implementation will be called.
This is broad topic, so any concerns or remarks will be highly appreciated.
Internal previews