On Sunday, 13 March 2016 at 19:37:42 UTC, ParticlePeter wrote:
I have a struct that privately warps an std.container.array. I would like to return a read-only reference of this array, it should not be duplicated. How can I do this?

Cheers, ParticlePeter

ref const(Array!Type) view(){}

Unless the result is explicitly cast later it can't me modified.


import std.stdio, std.container.array;

struct Foo
{
    private Array!int arr;
    ref const(Array!int) view()
    {
        return arr;
    }
}

void main(string[] args)
{
    Foo foo;
    auto a = foo.view.dup;
}

Reply via email to