www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - More Elegant Settable Methods?

reply jwatson-CO-edu <real.name colorado.edu> writes:
Hi,

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`?

```d
import std.stdio;

struct TestStruct{
     float /*------------------*/ a;
     float /*------------------*/ b;
     float function( TestStruct ) op;

     float run(){
         return op( this );
     }
}

void main(){
     TestStruct ts = TestStruct(
         2,
         3,
         function( TestStruct s ){ return s.a + s.b; }
     );

     writeln( ts.run() );
}
```
Jan 21 2023
next sibling parent reply ryuukk_ <ryuukk.dev gmail.com> writes:
```D
     TestStruct ts = {
         a: 2, b: 3,
         op: (s) {
             return s.a + s.b;
         }
     };
```


This simple! just like with C's designated initializers
Jan 21 2023
next sibling parent ryuukk_ <ryuukk.dev gmail.com> writes:
Oops i clicked "Send" too fast
Jan 21 2023
prev sibling parent jwatson-CO-edu <real.name colorado.edu> writes:
On Sunday, 22 January 2023 at 02:28:11 UTC, ryuukk_ wrote:
 ```D
     TestStruct ts = {
         a: 2, b: 3,
         op: (s) {
             return s.a + s.b;
         }
     };
 ```


 This simple! just like with C's designated initializers
Ah, I had forgotten about this handy initializer form! However, I may not have been clear. I was hoping that by assigning a function as a member to a `TestStruct`, that it would already have access to the other members of `TestStruct`. I can see now that this would not pass muster during type checking and is therefore a luxury afforded to dynamically typed languages only, like Python: ```python from types import MethodType class TestStruct: def __init__( self, A, B ): self.a = A self.b = B def op( self ): return self.a + self.b ts = TestStruct(2, 3) ts.op = MethodType(op, ts) print( ts.op() ) ```
Jan 26 2023
prev sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
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 79
Jan 29 2023
parent jwatson-CO-edu <real.name colorado.edu> writes:
On Monday, 30 January 2023 at 07:48:09 UTC, Salih Dincer wrote:
 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 79
So `delegate` looks closer to what I want. I am looking to implement a [Strategy Pattern](https://en.wikipedia.org/wiki/Strategy_pattern) in which I have an object with an update method which is settable at runtime, either during instantiation or after. The function you have assigned to `set` has access to the `S` object, just like I had wished; Thank you! The application is a [Braitenberg Vehicle](https://en.wikipedia.org/wiki/Braitenberg_vehicle) environment. There are a more than a few component types that are connected to each other in the same way and pass messages of a consistent type. I'd like not to write a struct/class for each component type; but rather write their common structure once and set their type-specific behavior as they are created. It looks like I can write a function for each component type and assign a `delegate` update strategy after the common struct has been created. This is what I had asked for. Thank you for this lesson.
Jan 31 2023