More Elegant Settable Methods?

Salih Dincer salihdb at hotmail.com
Mon Jan 30 07:48:09 UTC 2023


On Saturday, 21 January 2023 at 23:07:45 UTC, jwatson-CO-edu 
wrote:
> I am trying to create a struct with a settable method that has 
> access to the struct scope.
> Is this the only way?
> Is there a way to give access without explicitly passing `this`?

Why not use the delegate? What exactly do you want to do?  Set 
after constructor or assign delegate?

```d
struct S {
     float /*------------------*/ a;
     float /*------------------*/ b;

     void delegate(float x, float y) set;
     auto sum() { return a + b; }
}

void main() {
     S s;
     s.set = (x, y) {
         s.a = x;
         s.b = y;
     };
     s.set(10, 20);
     assert(s.sum == 30);
}
```

SDB at 79


More information about the Digitalmars-d-learn mailing list