First Draft: Static Single Assignment

Peter C peterc at gmail.com
Thu Dec 4 22:08:48 UTC 2025


On Thursday, 4 December 2025 at 15:18:18 UTC, jmh530 wrote:
> On Thursday, 4 December 2025 at 08:58:03 UTC, Walter Bright 
> wrote:
>> I've come to realize the following:
>>
>> ```d
>> void test() {
>>     final int x = 3;
>>     pragma(msg, typeof(&x));
>> }
>> ```
>> prints: const(int)*
>>
>> which is a more practical way to make this all work. I've made 
>> the changes to the PR.
>
> So taking the address of x gives a const pointer? Is `x` itself 
> const?

final is a storage a class. const is a type qualifier.

The final storage class does not modify the base type of the 
variable. The type of the variable x is still the underlying type 
- in this case, an int.

Although the variable's type isn't const(int), the compiler has 
to guarantee that a final variable cannot be modified. Therefore, 
when you take its address (&x), the resulting pointer must be a 
pointer to a read-only object to prevent modification through the 
pointer. The compiler implicitly adds const to the pointer's 
target type.

That final degrades to const in the presence of 
pointers/references, is the necessary tradeoff for getting the 
single assignment guarantee without having to change the type 
system itself.

void test()
{
     // const vs final

     const int z = 10;
     writeln("z is a ", typeof(z).stringof); // z is a const(int)
     writeln("&z is a ", typeof(&z).stringof); // &z is a 
const(int)*
     auto y = &z;
     writeln("y is a ", typeof(y).stringof); // y is a const(int)*
     writeln("&y is a ", typeof(&y).stringof); // &y is a 
const(int)**

     writeln;

     // All pointers derived from an expression involving a final 
variable
     // must point to const data to maintain the promise of single 
assignment.

     final int x = 3;
     writeln("x is a ", typeof(x).stringof); // x is a int
     writeln("&x is a ", typeof(&x).stringof); // &x is a 
const(int)*
     auto r = &x;
     writeln("r is a ", typeof(r).stringof); // r is a const(int)*
     writeln("&r is a ", typeof(&r).stringof); // &r is a 
const(int)**

}



More information about the dip.development mailing list