-
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
@@ -0,0 +1,4 @@ | |||
public interface IGetDuplicated<T> where T : IGetDuplicated<T> | |||
{ | |||
static virtual T operator ++(T other) => throw new NotImplementedException(); |
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'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.
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.
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?
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 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.
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