Re: Programming in D by Ali Çehreli

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 17 11:17:24 PDT 2016


On 10/17/2016 11:10 AM, vino wrote:
> Hi All,
>
>  As per the book named in the subject it states as below,so can some one
> guide me what is wrong in the below code nor correct if my understanding
> is wrong.
>
> Page 154
> immutable(int[]) specifies that neither the slice nor its elements can
> be modified.
>
> So which means that a element or a slice of any array can be modified,
> but the below example code is not working.
>
> import std.stdio;
> void main() {
> immutable(int[]) immSlice = [ 1, 2 ];
> immSlice ~= 3;
> immSlice[0] = 3; // Error's out at this point
> immSlice.length = 1;
> writeln(immSlice);
> }
> From,
> Vino.B

What version is your compiler? My version is DMD64 D Compiler v2.072.0-b2.

Trying the code with a recent compiler produces three compilation errors 
for the following three lines

     immSlice ~= 3;
     immSlice[0] = 3;
     immSlice.length = 1;

Error: cannot modify immutable expression immSlice
Error: cannot modify immutable expression immSlice[0]
Error: cannot modify immutable expression immSlice

Ali



More information about the Digitalmars-d-learn mailing list