www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct immutable data and dict

reply Patric Dexheimer <patric.dexheimer gmail.com> writes:
struct Test{
     immutable size_t id;
}

Test[string] dict;
Test[] array;

void main(){
     array~=Test(1);//work
     dict["teste"] = Test(1); //fail ??
}

"Error: cannot modify struct dict["teste"] Test with immutable 
members"
Why?
Oct 05 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 6 October 2016 at 01:23:35 UTC, Patric Dexheimer 
wrote:
 Why?
Because you'd be overwriting that immutable member. Structs just put structure around their contents, but it doesn't change their nature. That struct is no different than if you wrote `immutable size_t` as the value - and of course, overwriting that; changing that violates that promise that you won't change it. You could store pointers to those structs though, and overwrite the pointer.
Oct 05 2016
parent reply Patric Dexheimer <patric.dexheimer gmail.com> writes:
On Thursday, 6 October 2016 at 02:09:44 UTC, Adam D. Ruppe wrote:
 On Thursday, 6 October 2016 at 01:23:35 UTC, Patric Dexheimer 
 wrote:
 Why?
Because you'd be overwriting that immutable member. Structs just put structure around their contents, but it doesn't change their nature. That struct is no different than if you wrote `immutable size_t` as the value - and of course, overwriting that; changing that violates that promise that you won't change it. You could store pointers to those structs though, and overwrite the pointer.
But why i´m overwriting the struct if its the first time i´m putting it there? (like on the array).
Oct 05 2016
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 6 October 2016 at 03:05:18 UTC, Patric Dexheimer 
wrote:
 But why i´m overwriting the struct if its the first time i´m 
 putting it there? (like on the array).
The compiler doesn't know it is the first time (it doesn't follow the data from creation, it just looks at that individual line, and first time write is no different than later writes with the associative array index, whereas array append is different so it can tell).
Oct 05 2016
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 6 October 2016 at 03:05:18 UTC, Patric Dexheimer 
wrote:

 But why i´m overwriting the struct if its the first time i´m 
 putting it there? (like on the array).
There's a difference between initialization and assignment. ``` // Given this structure struct MyStruct { int x; } // Both of these lines are initializstion auto mystruct1 = MyStruct(10); MyStruct mystruct2; assert(mystruct1.x == 10); assert(mystruct2.x == 0); // And both of these are assignments. mystruct2 = MyStruct(11); mystruct1 = mystruct2; assert(mystruct2.x == 11); assert(mystruct1.x == 11); ``` If x were immutable, both initializations would succeed, but both assignments would fail.
Oct 05 2016
parent Patric Dexheimer <patric.dexheimer gmail.com> writes:
On Thursday, 6 October 2016 at 03:48:22 UTC, Mike Parker wrote:
 On Thursday, 6 October 2016 at 03:05:18 UTC, Patric Dexheimer 
 wrote:

 [...]
There's a difference between initialization and assignment. [...]
On Thursday, 6 October 2016 at 03:48:22 UTC, Mike Parker wrote:
 On Thursday, 6 October 2016 at 03:05:18 UTC, Patric Dexheimer 
 wrote:

 [...]
Hm, so in dict they are always assuming that i´m doing assignments.. Ok, more clear now. thanks.
Oct 06 2016