Helping with __mutable (which will be renamed to __metadata)
Timon Gehr
timon.gehr at gmx.ch
Mon Apr 15 11:17:55 UTC 2019
On 14.04.19 22:55, Suleyman wrote:
> On Sunday, 14 April 2019 at 00:38:16 UTC, Timon Gehr wrote:
>> I don't think it should be @safe. Rather, `pure` and `immutable`
>> should retain their meanings,
>
> It cannot be strongly pure. Same as we have weakly pure this is weakly
> immutable we're dealing with here.
> ...
No, the point is that whatever high-level rewrites you can deduce from
`immutable` and `pure` shouldn't be restricted by code that modifies
`__metadata`.
>> which implies that there are wrong ways to use `__mutable` (hence unsafe)
>
> can you elaborate?
>
struct S{
private __metadata x;
}
void foo(immutable ref S s)pure{
s.x += 1;
}
void main(){
immutable S s;
foo(s); // there is no reason for this call to happen
assert(s.x==1); // can't rely on this, it might also be 0
}
struct S{
private __mutable x;
}
int foo(immutable ref S s)pure{
s.x += 1;
return s.x;
}
void main(){
immutable S s;
int a=foo(s);
int b=foo(s); // could just reuse the previous result
assert(a!=b); // can't rely on this, might be the same
}
More information about the Digitalmars-d
mailing list