Poll: do you use @property semantics?

Salih Dincer salihdb at hotmail.com
Wed Sep 28 15:47:29 UTC 2022


On Monday, 26 September 2022 at 18:51:41 UTC, Ali Çehreli wrote:
> On 9/26/22 11:50, Dom DiSc wrote:
>
> > if x is a property
> > (having a getter and a setter), ++x should work
>
> That came up a couple of times on this thread.
>
> Sounds easy to my ears. Is there a blocker for the 
> implementation?
>
> Ali

A nested struct can  do this, but each field needs a new build. 
For example to `Inner z` in code:

```d
template foo(T)
{
   struct Inner
   {
     T bar;
     alias bar this;
     auto opUnary(string op)()
     if(op == "++")
     {
       ++bar;
       return this;
     }
   }
   Inner z;
}

struct Outer(T)
{
   T X, Y;

   mixin foo!T s;
   alias z = s.z;

   auto x() { return X; }
   auto x(T n) { return X = n; }

   auto y() { return Y; }
   auto y(T n) { return Y = n; }
}

import std.stdio;
void main()
{
   auto outer = Outer!int();
   with(outer)
   {
     x = 320;
     y = 240;
     z = 160;
     z++;

     writefln!"x = %s, y = %s, z = %s"
     (x, y, z); // x = 320, y = 240, z = 161
   }
}
```
SDB at 79


More information about the Digitalmars-d mailing list