Helping with __mutable (which will be renamed to __metadata)
Steven Schveighoffer
schveiguy at gmail.com
Mon Apr 15 12:32:17 UTC 2019
On 4/15/19 7:17 AM, Timon Gehr wrote:
> On 14.04.19 22:55, Suleyman wrote:
>> On Sunday, 14 April 2019 at 00:38:16 UTC, Timon Gehr wrote:
>>> 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
> }
Note that this is exactly the use case of reference counting. Which is
the main draw of having a mutable portion of an immutable. I would
hazard to guess that if the above has to be the semantics, this is DOA.
-Steve
More information about the Digitalmars-d
mailing list