On Tuesday, 2 February 2016 at 23:41:07 UTC, H. S. Teoh wrote:
Now, the same argument applies if immutable was used in place of const. However, the last line in main() illustrates why we need const rather than immutable in this case: we actually *want* to modify d.x in main(). We just don't want func2 to touch anything. So we can't use immutable -- since immutable means *nobody* can touch the data. So, const provides both the guarantee that func2 won't touch the data, thus allowing the aforementioned optimization, and also continues to let main() mutate the data at its leisure.

Ok, but what would differ using immutable instead of const in your example (AS ARGUMENTS)?

See:

import std.stdio;

        struct Data { int x; }
        auto func1(Data* d) { return d.x; }
        auto func2(const(Data)* d) { return d.x; }
        auto func3(immutable(Data)* d) { return d.x; }  

        void main() {
                Data d;
                auto value1 = d.x*func1(&d) + d.x;
                auto value2 = d.x*func2(&d) + d.x;
                auto value3 = d.x*func3(cast(immutable)&d) + d.x;
                d.x++;
                writeln(d.x++);
        }

Functions 2 and 3 are acting in the same way and different from what you said I can change the d.x in main.

Bubba.

Reply via email to