The more I use properties in C# the more annoyed I am with repetitions. Consider such code:
private SomeType field; public SomeType Field { get { return this.field; } set { // some logic with previous value this.field = value; // some logic with new value } }
Triggering events, logging data in a database, you name it — this pattern occurs over and over again. And the best you can come up is writing Wrapper
class and using it like here (still C#):
private Wrapper<SomeType> field = new Wrapper<SomeType>(); public SomeType Field { get { return this.field.value; } set { this.field.value = value; } }
Some advise the solution to this is using snippets which is hilarious idea — it is like saying that you can reduce the amount of code by typing faster. Besides I am not a monkey, so I look for the solution elsewhere.
Somewhere along AOP or Scala-like traits — I am thinking of property specialized overlays:
overlay Wrapper // implicit first generic parameter: T def set(value T) // some logic with previous value inner.set(value); // some logic with new value end end
Since overlay will be mixed with property we have to connect it somehow to the already existing setter or getter — all we need is inner
reference. And the usage could be reduced to this:
def field SomeType with Wrapper;
The type of the field
would be SomeType
, keyword with
does not change a type, it just indicates what wiring compiler should do behind the scenes.
It is just a vague idea, but it has to be done, yesterday my inner monkey wrote:
private SomeType field; public SomeType Field { get { return this.field; } set { // some logic with new value this.field = value; // some logic with previous value } }
So it is about time to make properties right.