Is remove safe using foreach

j my.email at gmail.com
Sun Dec 18 04:56:43 UTC 2022


On Monday, 12 December 2022 at 17:23:36 UTC, lili wrote:
> ```
> int[string] aa = ["ok":1, "aaa":2, "ccc":3, "ddd":4];
> 	foreach (k ; aa.byKey)
> 	{
> 		if (k == "aaa") {
> 		aa.remove(k);
> 		aa["ww"] = 33;
> 		}
>
> 		if (k == "ww") {
> 		aa.remove(k);
> 		aa["vv"] = 33;
> 		}
> 	}
>
> writeln(aa); // output ["ok":1, "ddd":4, "vv":33, "ccc":3] is ok
> ```



No. This isn't safe in any language. It doesn't even make sense 
technically. Use a method or inject. Try the language ruby. It is 
nice. Also I have heard good things about Java and Rust.


More information about the Digitalmars-d-learn mailing list