Struct immutable data and dict

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 5 20:48:22 PDT 2016


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.




More information about the Digitalmars-d-learn mailing list