D Properties when?
jmh530
john.michael.hall at gmail.com
Thu Dec 14 18:48:04 UTC 2023
On Thursday, 14 December 2023 at 18:08:52 UTC, Hipreme wrote:
> This is no news in the programming world, yet, we still don't
> have it.
>
> This is very important for I can define a good API in which I
> could increase my engine performance.
> With that, I could implement fields with dirty flags and not
> make the API looks dirty (no pun intended).
>
>
> Think of an object with X and Y position. I want to be able to
> when its value changes, it sets a dirty flag to true. The
> problem is that I'm unable to do `X+= 50` after that. I need to
> do `X = X + 50`, this is not news but still makes the API
> inconsistent and more verbose.
> I don't care how this is implemented, I only want this some day
> to not make it look ugly.
I'm not entirely sure what you mean, but what about something
like this:
```d
import std.stdio: writeln;
struct Direction
{
int x;
alias x this;
private bool _dirty;
void opOpAssign(string op: "+")(int rhs) {
x += rhs;
_dirty = true;
}
}
struct Foo
{
Direction x;
Direction y;
this(int x, int y) {
this.x = Direction(x);
this.y = Direction(y);
}
}
void main()
{
auto f = Foo(1, 2);
f.x += 50;
writeln(f.x._dirty);
writeln(f.y._dirty);
}
```
More information about the Digitalmars-d
mailing list