Make a variable single-assignment?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Nov 21 15:28:01 PST 2011


The only thing I can think of:

struct Once(T)
{
    this(T val)
    {
        i = val;
    }

    immutable T i;
    alias i this;
}

void main()
{
    Once!int i = 1;  // ok
    i = 4;  // ng
}

However it seems I've found a little hole in the system:

void foo(ref int x)
{
    x = 2;
}

void main()
{
    Once!int i = 1;  // ok
    foo(i);
    assert(i == 1);  // fail, changed to 2
}

Is this reported somewhere?


More information about the Digitalmars-d-learn mailing list