Simple immutable example doesn't work - why???

TheFlyingFiddle theflyingfiddle at gmail.com
Mon Nov 11 18:01:55 PST 2013


On Tuesday, 12 November 2013 at 01:36:12 UTC, TheFlyingFiddle 
wrote:
> Plz ignore my pervious statment. I guess i'm to tiered atm i 
> take it all back.

I rly am to tiered to think straight. Ingore the ignore post. I 
am sorry for being confusing.

auto a = new immutable(Test)[];
//The array is immutable and you cannot change the elements in 
any way.
//However the array can stil be rebound like so
a = new immutable(Test)[];
or like this
a ~= new immutable Test(3);

auto a new immutable Test[];
//The array is immutable and you cannot change the elements in 
any way.
b = new immutable Test[]; //This will fail.
//and so will this.
b ~= new immutable Test(3);

I don't think you can create rebindable immutable objects that 
are not wrapped by something. (like an array or the solution i 
present bellow)

Alternative solution:
Test is a rebindable immutable object (it's immutable in the same 
sense that strings are immutable)

class Test
{
    private immutable uint value;
    this(uint value)
    {
       this.value = value;
    }
}

unittest
{
   auto test = new Test(5);
   //The array is mutable but objects of type Test are immutable.
   auto array = new Test[5];
   array[0] = test;
}






More information about the Digitalmars-d-learn mailing list