First Draft: Static Single Assignment
Peter C
peterc at gmail.com
Fri Nov 21 23:21:46 UTC 2025
On Friday, 21 November 2025 at 15:04:27 UTC, Richard (Rikki)
Andrew Cattermole wrote:
>
> On 21/11/2025 10:48 PM, Peter C wrote:
>> [...]
>
> Variables defined in a loop get extracted outside of it.
>
> They shouldn't need a dedicated section, but good to bring it
> up!
But aren't you refering to a performance optimization?
I'm referring to semantic safety.
The purpose of using 'fixed' in the code below, is to prevent an
illegal or erroneous reference reassignment inside the loop body.
It's an independent concern, not at all related to optimization.
-----
module mymodule;
@safe:
private:
import std;
void main()
{
int[] arr = [100, 200, 300];
int extra = 999;
// Here, 'fixed' ensures that the loop variable remains bound
// to the correct array element throughout its iteration.
//
foreach (fixed ref element; arr)
{
// element is now bound as an alias to arr[i]
element = 500; // OK: modifying the value being referenced
-> arr[i]
element = extra; // ERROR: reassigning a fixed reference
binding is not allowed.
}
}
-----
More information about the dip.development
mailing list