Default implementations #8222
Replies: 1 comment 13 replies
-
Feels like something better suited to a source generator, one that can be configurable and apply policies to the emitted implementation of those methods. |
Beta Was this translation helpful? Give feedback.
13 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The idea is to allow a short language syntax to auto-implement default behaviors by the compiler. This proposal mainly focuses on functions. What comes to mind are things like
GetHashCode
orClone
. For me the latter is quite useful as currently there is no good way to clone objects (especially deep cloning).With records we now have classes with some default behavior auto-implemented already. So something similar was done before.
However the new keyword allows more flexible approaches and more control. And it could in theory be used for any type of object.
In this case the compiler would generate something like this:
It might also work with patterns like
IDisposable
orINotifyPropertyChanged
. So basically for well-known patterns which have a default behavior most of the time. This saves a lot of time and boilerplate code. Especially cloning and hash code generation would be enough to justify such language feature imo.So the accepted functions might be limited by the compiler and it can generate an error if the function has no default behavior. This can be extended later if needed.
This idea can of course be extended or adjusted. My goal is to allow cloning easier and save boilerplate code for classes.
Beta Was this translation helpful? Give feedback.
All reactions