On Monday, 11 November 2019 at 20:05:11 UTC, Antonio Corbi wrote:
Defining and using a constructor for WrapIntegerArray seems to work:

void main()
{
        import std.stdio;

        WrapIntegerArray arr1 = WrapIntegerArray(5);
        arr1[0] = 42;

        WrapIntegerArray arr2 = WrapIntegerArray(5);

        writeln(arr2[0]); // 42, not 0.
        writeln("arr1.wrap.arr.ptr = ", arr1.wrap.arr.ptr);
writeln("arr2.wrap.arr.ptr = ", arr2.wrap.arr.ptr); // identical
        assert(arr2[0] == 0); // fails
}

struct IntegerArray
{
        int[] arr;
        alias arr this;
        this(int l)
        {
                arr = new int[l];
        }
}

struct WrapIntegerArray
{
        this (int v) {
          wrap = IntegerArray(5);
        }

        IntegerArray wrap;
        alias wrap this;
}

Hope this helps.
Antonio

Thanks, Antonio. My problem is that the length of the array should be a built-in property of WrapIntegerArray (immutable in this case); what I'd actually want is a constructor without arguments. Jonathan's suggestion of using a factory function comes closest to that.

Bastiaan.

Reply via email to