-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: Optional methods #16064
Comments
What is the problem? What's wrong with the original code (using a default implementation in the base class)? |
@svick I still need to call them all and take the overhead of Task for each method. and I won't know if they are actually overridden or not. In fact, I might need async/sync pair for each, calling all of those default methods is just unnecessary. Using an interface per method doesn't seem to be appropriate either. |
await (obj as IMethod1Async)?.Method1Async() ?? Task.CompletedTask; |
@qrli It's not any better than calling the virtual method with a default implementation that returns Task.CompletedTask. I want to avoid Task.CompletedTask and interfaces. In fact, you are just addressing #7171, my |
@alrz Given your last example, what happens when I call await obj.Method1Async(); instead of await obj.Method1Async?(); Can an optional method return anything other than How about extending |
@bondsbw Re "Given your last example, what happens when I call" Depending on the implementation, you probably cannot invoke it as a regular method. "Can an optional method return anything other than Task" why not? (Also, async is not part of the method signature). "How about extending partial" Partial method is a compile-time feature, but here we're operating on virtual dispatch, so that wouldn't be possible. |
In that case, is there a need for the new invocation syntax?
What would the returned value be if not implemented?
Sure, |
In Swift it's required probably because they need them to be visually distinguishable. And also, since return types vary (e.g. bool to bool?) I think that's consistent with nullability operators. ?., ??
default(T) or default(T?) in case of value types.
I see, that's right. I didn't really thought about the syntax, a virtual method without a body just felt natural and that was the first thing I came up with. |
How would the compiler generate code without knowing what classes will derive from Can you post an example of how would this work for classes deriving from |
I explored some ways to implement this with interfaces (as mentioned in OP), a nullable delegate, a virtual property as condition, and template method pattern, it turns out that without clr support it wouldn't be that useful as a language feature and could be implemented via code generators. |
Assume that we have the following base class with some virtual methods for derived classes to override.
If we don't want to bother to call these methods if they weren't overridden, we should use interfaces,
Note that this won't work as
await
currently throws on null (#7171). So it'll be more verbose in practice.Swift has a concept of "optional methods" that may or may not be implemented in derived classes, e.g.
Note: This is only possible in Swift's protocols. So in C# it may or may not be suitable in class declarations.
Task is just an example here, in any case if our virtual method is not empty, we should use similar patterns. I'd welcome any other solution for this specific problem that doesn't need any language change.
The text was updated successfully, but these errors were encountered: