Make a variable single-assignment?

deadalnix deadalnix at gmail.com
Mon Nov 21 08:48:51 PST 2011


Le 21/11/2011 15:04, Alex Rønne Petersen a écrit :
> 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

You can create a struct Final you could use as Final!(type) variable;

Overloading opAssign should do whatever you need.

I don't think adding to the core language what could ba achived with a 
nice abstraction should be done.


More information about the Digitalmars-d-learn mailing list