View mode: basic / threaded / horizontal-split · Log in · Help
March 11, 2009
Aliasing immutable and mutable data
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);
}
March 11, 2009
Re: Aliasing immutable and mutable data
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)
   }
}
Top | Discussion index | About this forum | D home