www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to return a const handle (view) to a mutable member of an agregate

reply ParticlePeter <ParticlePeter gmx.de> writes:
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
Mar 13 2016
parent reply Basile B. <b2.temp gmx.com> writes:
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; }
Mar 13 2016
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
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
Mar 13 2016
parent reply JR <sunspyre gmail.com> writes:
On Sunday, 13 March 2016 at 20:13:03 UTC, Basile B. wrote:
 On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote:
 [...]
Basile beat me to it. Yes, ref const(Array!T) accessor. http://dpaste.dzfl.pl/cb2bc5cf9917
Mar 13 2016
parent ParticlePeter <ParticlePeter gmx.de> writes:
On Sunday, 13 March 2016 at 20:28:33 UTC, JR wrote:
 On Sunday, 13 March 2016 at 20:13:03 UTC, Basile B. wrote:
 On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote:
 [...]
Basile beat me to it. Yes, ref const(Array!T) accessor. http://dpaste.dzfl.pl/cb2bc5cf9917
Thank you very much, both of you. JR, that's excellent (additional) information :-) but my struct is not supposed to work as array, "wrapper" was a wrong expression. ref const(Array!T) accessor() will still do :-)
Mar 13 2016
prev sibling parent reply anonymous <anonymous example.com> writes:
On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote:
 ref const(Array!Type) view(){}

 Unless the result is explicitly cast later it can't me modified.
No, it can't be modified, period. Casting away const and then mutating is not allowed, it has undefined behavior.
Mar 13 2016
parent reply Chris Wright <dhasenan gmail.com> writes:
On Sun, 13 Mar 2016 21:14:59 +0000, anonymous wrote:

 On Sunday, 13 March 2016 at 20:10:57 UTC, Basile B. wrote:
 ref const(Array!Type) view(){}

 Unless the result is explicitly cast later it can't me modified.
No, it can't be modified, period. Casting away const and then mutating is not allowed, it has undefined behavior.
In theory, it can't be modified. As a practical matter, unions and casts will allow people to modify it. Decorating your code with safe is intended to prevent these holes (and other types of unsafe code). In practice, safe has some implementation flaws, but it will catch the straightforward cases -- and even some of the sneaky ones.
Mar 13 2016
parent anonymous <anonymous example.com> writes:
On Sunday, 13 March 2016 at 22:34:54 UTC, Chris Wright wrote:
 In theory, it can't be modified. As a practical matter, unions 
 and casts will allow people to modify it.
Saying that it *can't* be modified is slightly besides the point, yeah. It *must* not be modified. Casting away const and then mutating will probably just work as expected much of the time. But it's not allowed by the language. One must not do it. I think it's important not to give the impression that it's ok in practice to cast away const and then mutate. By the way, I don't think unions are in the exact same boat as casts here. With a union of const and mutable types, I'd say it's perfectly fine to mutate the data through the mutable one. Such a union is similar to having const and mutable pointers to the same data. Unions of immutable and mutable types are weird, though.
 Decorating your code with  safe is intended to prevent these 
 holes (and other types of unsafe code).
But even in non- safe code, the compiler doesn't just accept mutating through a const reference. It requires a cast, which is an explicit signal to just do what the programmer says. There's a similar signal for ignoring safe: trusted.
Mar 13 2016