-
Notifications
You must be signed in to change notification settings - Fork 300
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
Add support for [ObservableProperty] on partial properties #555
Comments
@Sergio0694 I haven't tried to track a language proposal before and couldn't immediately tell from the Partial Properties proposal/labels/information whether it is still expected to be available in the November 2023 time frame that you mentioned in #291 - since you have been tracking and adding to the the Partial Properties proposal/discussion any chance you can add any current information about time frame estimates here? Fwiw From what I've read here and in other issues Partial Properties seem like a great solution - just wondering if it is truly 'coming soon' or if this pushes into 2024 and beyond. Thanks for your time and any update! |
Hey @cmiles! We don't have any updates/ETAs to share at this time. Keep in mind that no C# features at all are confirmed until the moment they actually ship, so the same applies here. We're still actively looking into this though, just no news just yet 🙂 |
Its not in C# 12 |
@Sergio0694 since this did not make it into C#12 any chance that of any of the associated issues will be reconsidered for action in a more immediate time frame? I understand partial properties would facilitate the ideal implementation of things like 'Required' keyword support - but there is some functionality blocked by this that would be immediately useful and appreciated even if there were breaking changes in the future if/when partial properties make it into C#. |
What functionality is blocked? |
@Sergio0694 - It was my understanding that all of the issues/functionality listed below are blocked by wanting to wait and create an implementation based around the partial property language feature. For me the functionality the required keyword offers is particularly attractive as we work on enabling nullable support in our code - but I'm not aware of any way to combine [ObservableProperty] and required? Based on your question of 'What functionality is blocked?' maybe I am mistaken and there is a feature or approach that I'm not aware of to use [ObservableProperty] and required together - if so apologies for the wasted time/space in this issue but would very much appreciate a link to get me started! Add support for "required" and "virtual" keywords to property generator · Issue #679 · CommunityToolkit/dotnet |
No functionality is actually blocked. You can easily use all that — just manually define the properties 🙂 I understand the frustration for wanting a simpler way to do that, and I do agree that of course using the generator is better. But I don't think it's a good idea to dedicate resources to implement all that functionality when we might very well have partial properties out in preview soon, especially because then we'd still have to keep supporting that "workaround" to avoid breaking consumers. Rather, it makes more sense to just wait for partial properties to be available in preview, add support for those, and then release a preview of the MVVM Toolkit too so that people wanting to try those out can do so as well. |
@Sergio0694 No frustration here - seems like we just have a different evaluation of the issue. Fwiw I would still '+1' working on a solution now - but respect and understand your reasoning and decision and no doubt you have 100% better insight on the odds of partial properties arriving in a near future preview. I hope this issue will continue to be re-evaluated if for some reason partial properties, or something equivalently useful for generators, don't arrive for the next version of C#. Thanks for the prompt replies - appreciated! |
This was not added to C# 12. Is there any reason to not implement the other more traditional alternatives using attributes? |
This exact question and the answer for that are in the messages right above yours — the reason has not changed and it's the same as before 🙂 |
Partial properties are just in proposel state, it's not confirmed that they will be release any time soon (or at all). I don't think that it is a good idea to wait for some unknown future time and close all releated issues. I understand your argument, that the current solution is a workaround, but it's a workaround that already exists, so adding more features to the already existing / working "workaround" would be better for every current user of the toolkit. You can declare it obsolete as soon as partial properties are implemented (if ever) and then you don't have to worry about maintaning it in the future. Would it really take that many resources to add a additional parameter to control the accessor kind of the generated property to justify waiting for partial properties? |
@AignerGames is right on. I’ve had similar thoughts in the past about this issue. It could be years or never, yet the value is high and the effort seems at least relatively low. Worth the trouble of a depreciation period if needed. |
Personally, I don't use generators in MVVM Toolkit at all only for this sole issue. |
@Sergio0694 frustration here. The fact that It's becoming increasingly difficult to convince people (and believe myself) that I did a smart thing in choosing this library. To me it doesn't matter how, whether via attribute or some future C# language feature. Just add the ability and make these features useful for more than just demos. |
I don't have experience with this code base, so my information could be wrong, but after a quick check I think that only a single place of the generator would have to be changed here to add the modifier type. Well and the code to define the enum somewhere, modify the attribute to accept the enum and maybe unit tests. |
For my organization and project, it matters a lot. The project I'm working on isn't just an app, but also a collection of libraries that other internal development teams will use. There is a client API library, a view model library, and a UI component library, and other devs may use all of it or just parts. As the provider of these libraries, it is a very big deal that these libraries be hard to use incorrectly. If Intellisense shows that something can be done/set/called then it must be a valid operation. That is the point of object-oriented encapsulation/data hiding, and all these |
What about a separate attribute? Something like: [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
public sealed class SetterSettingsAttribute : Attribute
{
public SetterSettingsAttribute(Accessibility accessibility) => Accessibility = accessibility;
public Accessibility Accessibility { get; }
//...
}
public enum Accessibility
{
//...
Private
//...
}
public partial class Sample : ObservableObject
{
[ObservableProperty, SetterSettings(Accessibility.Private)]
private string name;
public Sample(string name) => Name = name;
} |
Using an attribute is strictly worse than using partial properties. |
I think we all agree that partial properties would be the best solution, but they don't exist yet and will not be released anytime soon. |
They are quite literally already merged in the Roslyn repo and will be available in VS 17.11 Preview 3, out shortly 😄 |
Yes the preview version, but most people will not use a preview version. The next full release is currently planned for November 24 (if no delays happen) so almost 2 years after this issue was opened. I agree that we can wait now, but adding a "workaround" 2 years ago would still have been better, since all the closed requests over the years prove that there was a use case for it. |
Hindsight is 20/20. Partial properties were pushed back multiple times, we didn't know it'd take this long at the time 🙂 |
I guess you are more optimistic than me, I don't trust any of their release dates until the feature is implemented, because I waited for many C# features in the past that were pushed back multiple times or only proposed and then never worked on until this day 😂 |
Agreed. If you can use partial properties, then you should do so =) But they are not always available. And I do not mean that they are not implemented yet. For example, the codebase may be pinned to C# 11 for some reason. As stated in the https://github.com/dotnet/csharplang/blob/main/proposals/partial-properties.md article, the programmer must specify the access modifier when implementing partial properties: partial class C1
{
public partial string Prop { get; private set; }
public partial string Prop { get => field; private set => field = value; }
} Thus, the logic for generating the access modifier is required in this case too. In the case of partial properties, the information about required access modifier is available in the defining declaration. In turn, in the case of using fields, similar information could be taken from a special attribute. So we get
[ObservableProperty]
public partial string Name { get; private set; }
[ObservableProperty, WithPrivateSetter]
private string name; If I understood correctly from the discussion, you do not want to modify the However, all this makes sense if you do not plan to abandon the approach with fields. |
Hi! I just came across this issue as I was looking for a solution to a problem that might be solved by this. I would definitely appreciate if a feature like this were available. I also just found out that the C# updates in .NET 9 Preview 6 actually includes the partial properties! A first step might be for the |
Generating a partial property wouldn't really do anything, other than making your code fail to compile 🙂 As for the real support in the MVVM Toolkit, I'm waiting to see whether |
Ok, then my knowledge is not reaching far enough. I hope something like this can be implemented though. |
Ok I played around with the Preview a little bit. I guess the Introduction of |
@LE-MarkusW I don't really know your use case but if you haven't already it might be worth your time to look at Metalama for code generation. Metalama is a paid tool with a free tier - Pricing, License Information. I used it to write my own INotifyPropertyChanged a little over a year ago in order to take advantage of the required keyword and and now use it for a few other generation tasks including Commands. For my simple requirements getting to working code was pretty quick. Metalama isn't my focus - you can see much more beautiful code in the Metalama documentation - but here is my current code PointlessWaymarks.LlamaAspects. |
It would be better to warn users on the old field usage as well and provide a code fix to help developers migrate from attributes on fields to attributes on partial properties after we ship the support. |
I plan on adding a code fixer for that as well, yes 🙂 |
hello, when can we get this with dotnet 9 released? |
|
When is a stable version released with this? |
Overview
This issue tracks adding support for partial properties to the
[ObservableProperty]
generator. This is blocked on dotnet/csharplang#6420, so support for this will have to wait for that first (hopefully in the C# 12 timeframe).API breakdown
The changes are pretty straightforward: properties will also be allowed on
[ObservableProperty]
:Analyzers will also need to be added to ensure that the annotated properties are partial and valid.
Usage example
Code like this:
Will generate the following:
This will also enable support for declaring custom accessibility modifiers for property accessors.
Progress tracking
The text was updated successfully, but these errors were encountered: