Support for field modification flagging #110726
Replies: 2 comments
-
This will definitely NOT be considered as a runtime feature. Fields are a part of memory accessing model. They are meant to be plain memory accesses, with optional atomic/volatile modifiers. Additional operations won't ever be considered.
Note that public fields are recommended against for most scenarios, except interop cases. Fields are directly tightened with implementation details. Properties are allowed to change their implementation without breaking compatibility. These patterns are handled by high level languages like C#, for example the MVVM Source Generator generates the following pattern: public partial class A : ObservableObject
{
[ObservableProperty]
private int f1;
[ObservableProperty]
public partial int F2 { get; set; }
} /// <inheritdoc cref="f1"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.4.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int F1
{
get => f1;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(f1, value))
{
OnF1Changing(value);
OnF1Changing(default, value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.F1);
f1 = value;
OnF1Changed(value);
OnF1Changed(default, value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.F1);
}
}
}
/// <inheritdoc/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.4.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial int F2
{
get => field;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(field, value))
{
OnF2Changing(value);
OnF2Changing(default, value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.F2);
field = value;
OnF2Changed(value);
OnF2Changed(default, value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.F2);
}
}
} The generated element must be property then, to support any additional operation. |
Beta Was this translation helpful? Give feedback.
-
You can use https://github.com/Fody/PropertyChanged to achieve this "is dirty" feature. |
Beta Was this translation helpful? Give feedback.
-
Forgive me if the title didn't accurately summate the concept, but there is a fairly regular pattern that I've seen in projects that looks in some form or another like this (using T as a wildcard, not as an actual generic type)
where the only reason that the field cannot be public is because we wanted to track when it was modified, so my proposal is this: An attribute 'MarkWhenModified' (or equivalent name) that can be applied to fields to automate this process, an example of how this would work [related to game development] is this:
such a pattern would allow for more concise code when setting values of more 'complex' types [like vectors] as with the first pattern such changes would need to be made like the following
but the new method would allow us to simply do
EDIT: Before someone suggests that this belongs elsewhere, this technically belongs to multiple 'topics' since a feature like this would need to be integrated into the runtime itself, as well as be an addition to the language library
Beta Was this translation helpful? Give feedback.
All reactions