Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 17, 2016 Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
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 |
October 17, 2016 Re: Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino | 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
|
October 17, 2016 Re: Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino | 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.
|
October 17, 2016 Re: Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | On Monday, 17 October 2016 at 18:20:00 UTC, cym13 wrote:
> On Monday, 17 October 2016 at 18:10:01 UTC, vino wrote:
>> [...]
>
> 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.
I would expect him to see the first error on `immSlice ~= 3;`.
|
October 18, 2016 Re: Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | 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);
}
|
October 18, 2016 Re: Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 17 October 2016 at 18:17:24 UTC, Ali Çehreli wrote:
> On 10/17/2016 11:10 AM, vino wrote:
>> [...]
>
> 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
Hi Ali,
I am using DMD64 D Compiler v2.071.2-b5
|
October 18, 2016 Re: Programming in D by Ali Çehreli | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino | On 10/17/2016 11:48 PM, vino wrote:
> 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);
> }
>
What is meant in that section in the book is
1) We cannot modify elements of an immutable slice
2) We cannot modify an immutable slice (e.g. cannot add elements to it)
That's the reason for the following three compilation errors:
Error: cannot modify immutable expression immSlice
Error: cannot modify immutable expression immSlice[0]
Error: cannot modify immutable expression immSlice
Your new code has another compilation error here:
int[] Array = [ 1, 2, 3, 4 ];
immutable(int[]) immSlice = Array[ 0 .. 2 ]; // <-- ERROR
Error: cannot implicitly convert expression (Array[0..2]) of type int[] to immutable(int[])
That's because immSlice wants to guarantee that it has immutable elements. For that to be true, it cannot share elements of a mutable slice. Otherwise, elements could be modified through Array, which would also change immSlice's elements. (Remember that Array and immSlice would be sharing elements.)
Ali
|
Copyright © 1999-2021 by the D Language Foundation