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

2016-03-13 Thread anonymous via Digitalmars-d-learn

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.


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

2016-03-13 Thread Chris Wright via Digitalmars-d-learn
On Sun, 13 Mar 2016 21:14:59 +, 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.


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

2016-03-13 Thread anonymous via Digitalmars-d-learn

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.


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

2016-03-13 Thread ParticlePeter via Digitalmars-d-learn

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 :-)


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

2016-03-13 Thread JR via Digitalmars-d-learn

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



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

2016-03-13 Thread Basile B. via Digitalmars-d-learn

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


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

2016-03-13 Thread Basile B. via Digitalmars-d-learn

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;
}


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

2016-03-13 Thread ParticlePeter via Digitalmars-d-learn
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