Optional Async Partial Method that returns a Task #7083
Replies: 8 comments 47 replies
-
You could use source generators to fill in a default implementation of the method that returns a completed task. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to open this back up for discussion? I believe being able to specify |
Beta Was this translation helpful? Give feedback.
-
There appears to be a very good discussion on this, I hope the below is a useful contribution. I know there are still problems to consider, but i think this feature would be a plus to the language.
Is that really the case though? Would it not be more accurate to say that the purpose of partial methods is to support generated code? The Zero cost requirement is a desired implementation detail, not the purpose, and while that goal is a good one for any language feature, pretty much everything you can do with async/await is technically possible without those keywords, but that non-zero cost feature greatly improves developer efficiency. It is also worth considering that many of the scenarios which would benefit from this feature are already incurring these costs, but without the benefits to developer productivity, so while from a theoretical perspective it is not zero cost, from a practical perspective it may be.
it would only be unclear if undocumented though right? if the documentation clearly calls out what it does, it will be "obvious" what the code will do ( for example
The problem with this code is it puts the weirdness on the developer side instead of on the generated code side. From that perspective the above is definitely inferior to |
Beta Was this translation helpful? Give feedback.
-
I also just want to point out that nested functions are not unit testable on their own. Forcing developers through that pattern is a net-negative. |
Beta Was this translation helpful? Give feedback.
-
Sorry I marked an answer but It's probably up to the experts to selecte an answer :) |
Beta Was this translation helpful? Give feedback.
-
Example gist of an empty async method source generator as a temporary workaround: https://gist.github.com/xiety/0ae30d718bbf6942e876a5dc230c573f |
Beta Was this translation helpful? Give feedback.
-
I have kind of run into situation similar to this. The way I handle it is having my generated classes derive from a base class that has empty virtual methods that do nothing. In the event that I want to enhance the generated code, I just add a partial class that overrides the empty base class method. |
Beta Was this translation helpful? Give feedback.
-
What about to use atributes to indicate when a partial method should be implemented or not? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, partial methods that are optional can only have a return type of void or a non-awaitable async void, which limits their usefulness. I use partial methods to add custom logic to generated code when necessary. However, I face a problem when I want to use awaitable async methods in this custom code. As a solution, I propose introducing an optional Async Partial Method that returns a Task instead of void.
Beta Was this translation helpful? Give feedback.
All reactions