www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [idea] Mutable pointee/ RCString

reply Iakh <iaktakh gmail.com> writes:
Is it hard to make pointee data mutable?
E.g. if have:
------
struct RCString
{
     private char[] data;
     private  mutable int* counter;
}
------
So for optimiser (in case of immutable) this looks like
------
struct RCString
{
     private char[] data;
     private  mutable void* counter; // pointer to garbage
}
------
Feb 07 2016
parent Iakh <iaktakh gmail.com> writes:
On Sunday, 7 February 2016 at 14:00:24 UTC, Iakh wrote:

Explanations:
As far as "immutable" transitive:
------
immutable RCString str;
*str.counter++; // Impossible/error/undefined behavior(with const 
cast)
------
Language defines immutable to do some optimizations based on true
constness of str, fields, and variables pointed by fields. But if
pointer would be treated by optimizer(not by GC) as void* (or 
size_t)
pointee "true constness" does not matter.

The only drawback is the immutable function that reads  mutable 
field
can't be  pure because it reads "global variable".
Feb 07 2016