digitalmars.D.learn - More Elegant Settable Methods?
- jwatson-CO-edu (24/24) Jan 21 2023 Hi,
- ryuukk_ (9/9) Jan 21 2023 ```D
- ryuukk_ (1/1) Jan 21 2023 Oops i clicked "Send" too fast
- jwatson-CO-edu (20/29) Jan 26 2023 Ah, I had forgotten about this handy initializer form! However, I
- Salih Dincer (22/26) Jan 29 2023 Why not use the delegate? What exactly do you want to do? Set
- jwatson-CO-edu (19/46) Jan 31 2023 So `delegate` looks closer to what I want. I am looking to
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
```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
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 initializersAh, 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
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
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: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.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 31 2023