Make a variable single-assignment?

Timon Gehr timon.gehr at gmx.ch
Mon Nov 21 08:41:31 PST 2011


On 11/21/2011 03:04 PM, Alex Rønne Petersen wrote:
> Hi,
>
> Is there any way to make a variable single-assignment, regardless of its
> type? I.e.:
>
> void foo()
> {
> <some magical keyword?> int i = 0;
> i = 2; // Error: i cannot be reassigned
> }
>
> I realize const and immutable will do this, but they are transitive and
> infect the type, which I do *not* want. I simply want the variable to be
> single-assignment. Is it possible?
>
> - Alex

How should that be possible without infecting the type?

void main(){
     <magical keyword> int i = 0;
     auto a = &i;
     *a = 2; // oops...
}


More information about the Digitalmars-d-learn mailing list