How to return a const handle (view) to a mutable member of an agregate

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 13 13:13:03 PDT 2016


On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote:
> 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;
> }

...and any attempt will produce an error, like in my sample:

Error: mutable method std.container.array.Array!int.Array.dup is 
not callable using a const object


More information about the Digitalmars-d-learn mailing list