Making array elements truly const/immutable

Era Scarecrow rtcvb32 at yahoo.com
Thu Aug 2 05:35:55 PDT 2012


On Thursday, 2 August 2012 at 11:42:21 UTC, Joseph Rushton 
Wakeling wrote:
> Sure.  What I'm looking for is not some absolute and 
> unachievable prohibition, but a safety-checking mechanism that 
> would be flagged by the compiler.  The real problem was that I 
> couldn't just set bar = foo without bar being const, but didn't 
> know how to do this without the cast, which violates safety.
>
> Since to!()() allows me to make that copy -- or is it 
> copy-on-write? -- in a safe way, that seems a perfectly viable 
> solution.

  to!()() should make a copy from immutable to non, and vice-verse.

  COW can be easy to implement in functions, like string 
processing where once you've decided to change it, you make a 
mutable copy, modify it, then cast it back to immutable returning 
the new string.

http://dlang.org/memory.html#copy-on-write

  COW outside of a function (as a type) could be a little 
different; a single flag could handle that case if it should copy 
or not; But having a structure to handle it may more complex. If 
there is not yet a COW container, I'll begin working on one.


More information about the Digitalmars-d-learn mailing list