www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Aliasing immutable and mutable data

reply dsimcha <dsimcha yahoo.com> writes:
Is doing something like:

auto someInstance = new immutable(SomeClass);

considered to be casting, where all bets are off, or is it supposed to be
safe?  If it's supposed to be safe,  below is an example of where it's not.
If it's supposed to be like casting, then what's a safe way of creating
immutable class instances?

import std.stdio;

uint[] array;

class A {
    uint[] AArray;

    this(uint[] inArray) {
        AArray = inArray;
    }
}

void main() {
    array = new uint[10];
    auto bar = new immutable(A)(array);
    writeln(bar.AArray);
    array[0] = 1;
    writeln(bar.AArray);
}
Mar 11 2009
parent "Denis Koroskin" <2korden gmail.com> writes:
On Wed, 11 Mar 2009 16:18:17 +0300, dsimcha <dsimcha yahoo.com> wrote:

 Is doing something like:

 auto someInstance = new immutable(SomeClass);

 considered to be casting, where all bets are off, or is it supposed to be
 safe?  If it's supposed to be safe,  below is an example of where it's  
 not.
 If it's supposed to be like casting, then what's a safe way of creating
 immutable class instances?

 import std.stdio;

 uint[] array;

 class A {
     uint[] AArray;

     this(uint[] inArray) {
         AArray = inArray;
     }
 }

 void main() {
     array = new uint[10];
     auto bar = new immutable(A)(array);
     writeln(bar.AArray);
     array[0] = 1;
     writeln(bar.AArray);
 }
http://www.digitalmars.com/d/2.0/accu-functional.pdf IIRC, the following syntax was proposed: auto foo = new invariant Foo(arg1, arg2); class Foo { this() { // mutable version } this() invariant { // invariant version (with check and stuff, not implemented yet) } }
Mar 11 2009