Thread overview
More Elegant Settable Methods?
Jan 22
ryuukk_
Jan 22
ryuukk_
6 days ago
Salih Dincer
4 days ago
jwatson-CO-edu
January 21

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?

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() );
}
January 22
    TestStruct ts = {
        a: 2, b: 3,
        op: (s) {
            return s.a + s.b;
        }
    };

This simple! just like with C's designated initializers

January 22

Oops i clicked "Send" too fast

January 26

On Sunday, 22 January 2023 at 02:28:11 UTC, ryuukk_ wrote:

>
    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:

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() )
6 days ago

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?

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

4 days ago

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?

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 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 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.