So, why when i want to capture a reference |*value| in a ‘if ()’ condition (optional or error union) i can just do ‘if(opt_value) |*value|’, but if it is in a ‘for ()’ loop, i need to use & ‘for (&array) |*value|’ ? I tried to find the answer, even using AI, but i didn’t get the concept. Any help is welcome!
The simple answer is most likely just “because it was implemented inconsistently”.
The slightly longer answer is probably that for(array) makes a temporary (and therefore const) copy of the array. That’s why you cannot get a mutable pointer to its elements.
(Well that’s not quite true, as the compiler will often enough optimize away the copy, but semantically it should act like a copy)
Also note that for(slice) |*value| works, so this is an edge case only when using arrays.