Strange behavior using array of structures

sarn sarn at theartofmachinery.com
Wed Apr 4 10:09:41 UTC 2018


On Wednesday, 4 April 2018 at 10:00:18 UTC, Orfeo wrote:
>       foreach (l; log) {
>          l.run;
>       }

Try making this "foreach (ref l; log) {".

Structs are value types in D, so by default they're copied when 
you assign them to another variable (in this case l).  That means 
run() is modifying a copy that gets thrown away each time.  "ref 
l" makes l into a reference to the original struct instance.


More information about the Digitalmars-d-learn mailing list