On Monday, 17 October 2016 at 18:20:00 UTC, cym13 wrote:
On Monday, 17 October 2016 at 18:10:01 UTC, 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

I don't see what you don't understand, you said it yourself: "neither the slice nor its elements can be modified". So you can't modify the elements of an immutable array. immSlice is an immutable array of which you are trying to modify an element.

Hi,

Thank you for your reply, can you address why the below code is not working

import std.stdio;

void main() {
int[] Array = [ 1, 2, 3, 4 ];
immutable(int[]) immSlice = Array[ 0 .. 2 ];
writeln("1st : ", immSlice);
immSlice ~= 3;
writeln("2nd : ", immSlice);
immSlice[0] = 3;
writeln("3rd : ", immSlice);
immSlice.length = 1;
writeln("4th : ",immSlice);
}

Reply via email to