On Monday, 9 June 2025 at 07:24:41 UTC, confuzzled wrote:
Hello community,

Is it possible to accomplish the following using ref instead of pointers? If so, please share an example.

import std.stdio;

// Represents our large, external data source (like TimeSeries)
struct DataSource
{
    int[3] data;
}

// Represents our Backtester engine
struct Engine
{
// It STORES a pointer to its data source. This is its "memory".
    const(DataSource)* dataSource;

    // The run method USES the stored pointer.
    void run()
    {
        writeln("Engine running...");
        if (dataSource is null)
        {
            writeln("  Error: Data source not configured!");
            return;
        }

        // We access the data through the remembered pointer.
        foreach (i, val; dataSource.data)
        {
            writef("  Processing data point %d: %d\n", i, val);
        }
    }
}

void main()
{
    writeln("--- Running Pointer Member Example ---");

    // 1. Create our data. It lives here in main.
    auto myData = DataSource([10, 20, 30]);

    // 2. Create our engine. It starts empty.
    auto myEngine = Engine();
    writeln("Engine created. Has it been configured yet?");
    myEngine.run(); // Will fail because the pointer is null

// 3. Configure the engine. We give it the ADDRESS of our data.
    //    The engine stores this address in its pointer member.
    writeln("\nConfiguring engine...");
    myEngine.dataSource = &myData;

    // 4. Later, we run the engine. It can now work because it
    //    remembered the address of myData.
    writeln("\nRunning configured engine...");
    myEngine.run();
}

Thanks,
-- confuzzled

The hard part is the `foreach` the simple answer is throw a ref before `val`.

Define your data to impliment ranges... but then you lose indexing on foreach
You can then use enumerate... but then you lose ref

Drop range entirely and you can interface with foreach with opApply, but then you dont get map/reduce.

---

Ranges do not support indexing in truth(enumerate is fundamentally incorrect after a filter) and foreach is opinionated about ranges.

the tradeoffs as I understand them:

1. use raw arrays, `foreach(i, ref v` is discouraged(there was a stupid breaking change) but the community will fight tooth and nail on the subject

2. make your own enumerate with a ref index(refness on tuples)

3. define opApply with 2 arguments(delegates dont share the shared refness issues of tuples)

Reply via email to