Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

BartoszKlonowski
Copy link
Contributor

@BartoszKlonowski BartoszKlonowski commented Oct 19, 2024

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 the static virtual example as well as the Duplicate type implementing that interface.
The implementation of the Duplicate struct is (on purpose) very similar to already existing RepeatSequence type, so that it bases on the reader's understanding and provides them with the comparison between static abstract and static 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

📄 File 🔗 Preview link
docs/csharp/whats-new/tutorials/static-virtual-interface-members.md docs/csharp/whats-new/tutorials/static-virtual-interface-members

@BartoszKlonowski BartoszKlonowski requested review from BillWagner and a team as code owners October 19, 2024 20:48
@dotnetrepoman dotnetrepoman bot added this to the October 2024 milestone Oct 19, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates PR is created by someone from the .NET community. label Oct 19, 2024
@@ -0,0 +1,4 @@
public interface IGetDuplicated<T> where T : IGetDuplicated<T>
{
static virtual T operator ++(T other) => throw new NotImplementedException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to think through this and use a different example for a static virtual (not abstract) method. That's because in practice, if the only reasonable implementation in the interface declaration is to throw a NotImplementedException, that indicates that it should be abstract.

Maybe consider an interface that provides < and >, but assumes that T implements IComparable<T>. Something like:

public interface IArithmeticComparable<T> where T : IComparable<T>
{
    static virtual bool operator > (T left, T right) => left.CompareTo(right) > 0;

// etc.
}

I know that adds a bit more concepts to explain, so I'm open to any alternative example. That's just the first obvious one that came to my mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is valid point, @BillWagner, thanks! But it looks like my knowledge is missing something, because my question is: What would be the use case of such implementation? In other words how to call the static virtual defined in an interface without any definition in the type implementing that interface? I know I'm asking kinda for an implementation details, but can you point me some examples of such use cases or implementations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's difficult to have a good scenario here @BartoszKlonowski

That's why I suggested the < and > operators. That's the one scenario I could think of where this works. It's not great, but it's reasonable.

Or, since any static method could be used, something like this might work:

public interface ISingleton<T> where T : new()
{
    public static virtual T Instance
    {
        get
        {
            field ??= new T();
            return field;
        }
        private set
        {
            field = value;
        }
    }
}

I have concerns here as well, because it uses a preview feature. But maybe we can adapt it somehow to do something like Lazy<T>, but use a static virtual method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-csharp/svc whats-new/subsvc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One of the samples should show a static virtual method
2 participants