First Draft: Static Single Assignment
Meta
jared771 at gmail.com
Fri Nov 28 00:40:06 UTC 2025
On Thursday, 27 November 2025 at 22:23:33 UTC, Peter C wrote:
> On Thursday, 27 November 2025 at 19:41:18 UTC, Jonathan M Davis
> wrote:
>>
>> ...
>> Of course, personally, I don't think that this feature adds
>> enough value to be worth adding at all, but it does feel very
>> much like something being welded onto the language to solve a
>> corner case that doesn't work with const rather than having a
>> more holistic solution.
>>
>> ...
>> - Jonathan M Davis
>
> Here is a 'simple' example of its value:
>
> class Data { int value = 1; }
>
> void main()
> {
> // const Data c = new Data();
> //c.value = 42; // Error: cannot modify `const` expression
> `c.value`
> //c = new Data(); // Error: cannot modify `const`
> expression `c` c = new Data();
>
> final Data c = new Data();
> c.value = 42; // fine.
> c = new Data(); // Error (SAA): Cannot reassign a 'final'
> variable.
>
> // .....
>
> // Pass the fixed reference to the performCheck function
> performCheck(c);
>
> // .....
>
> // We have a guarantee that here, 'c' will still refer to
> the same object it was initialized with
> writeln("\nValue: ", c.value);
>
> }
>
> // The compiler guarantees that performCheck cannot
> // introduce a side effect by reassigning the reference.
> //
> void performCheck(final Data obj)
> {
> obj = new Data(); // Error (SAA): Cannot reassign a
> 'final' variable.
> obj.value = 1; // fine. (Mutating the object is allowed)
> }
The compiler can already guarantee that, because the reference to
c is passed by value. To pass it by reference and introduce such
a side effect, the function would have to accept a ref Data.
More information about the dip.development
mailing list