First Draft: Static Single Assignment
Walter Bright
newshound2 at digitalmars.com
Thu Dec 4 09:06:34 UTC 2025
On 12/3/2025 5:10 PM, Jonathan M Davis wrote:
> If what you want is truly single assignment, then that's not a question of
> mutation. It's a question of assignment.
I cannot see why `final int x = 3; ++x;` should be legal.
I have not attempted to work through exactly how final would affect struct
fields. So I'm not prepared with a solid opinion on it. But even if it does not
work for fields, it remains useful for variables.
> I also strongly question that having the compiler enforce any form of single
> assignment is worth the trouble, particularly since I'm unaware of any
> actual benefits that it might bring to code generation.
It brings zero benefits to code generation. Its purpose is to make the code more
understandable. Many, many times I will do a search to see if a variable I set
at the beginning of a long function is modified in the guts a page away.
I've read multiple accounts online in other languages that people like this
diagnostic capability.
> I also cringe to think how it would work in generic code if classes are
> treated as head-const thanks to the fact that the have a reference, but
> structs are treated as full-on const, because they live on the stack. If
> classes are treated as fully const, then that problem probably goes away,
> but it also means that final is basically useless for anything other than
> primitive types.
Unlike C++, structs and classes are completely different. A final on a class
would affect only the class reference. A final on a struct will only affect its
fields.
More information about the dip.development
mailing list