More Elegant Settable Methods?

2023-01-21 Thread jwatson-CO-edu via Digitalmars-d-learn

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() );
}
```


Re: More Elegant Settable Methods?

2023-01-21 Thread ryuukk_ via Digitalmars-d-learn

Oops i clicked "Send" too fast



Re: More Elegant Settable Methods?

2023-01-21 Thread ryuukk_ via Digitalmars-d-learn

```D
TestStruct ts = {
a: 2, b: 3,
op: (s) {
return s.a + s.b;
}
};
```


This simple! just like with C's designated initializers




Re: More Elegant Settable Methods?

2023-01-26 Thread jwatson-CO-edu via Digitalmars-d-learn

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() )
```


Re: More Elegant Settable Methods?

2023-01-29 Thread Salih Dincer via Digitalmars-d-learn
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


Re: More Elegant Settable Methods?

2023-01-31 Thread jwatson-CO-edu via Digitalmars-d-learn

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.