On Wed, 11 Mar 2009 16:18:17 +0300, dsimcha <[email protected]> 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)
   }
}

Reply via email to