Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 2 06:54:28 PDT 2008


"Janice Caron" wrote in message
> It hardly needs a detailed explanation. In fact it's trivial to
> understand. Watch:
>
>    // in C++
>    void f(const C &c)
>    {
>        c.some.deeply.nested.but.reachable.var++;
>    }
>
> In a multithreaded program, a thread switch may occur between the read
> and the write of the read/write cycle that is ++. If that happens,
> you're screwed.
>
>    // in D
>    void f(const C c)
>    {
>        c.some.deeply.nested.but.reachable.var++; /*ERROR*/
>    }
>
> In D, such dodgy code just won't compile.

int x;

struct Var
{
    const int opPostInc()
    {
        return x++;
    }

    // when it is supported
    // int opImplicitCast() { return x; }
}

struct Reachable
{
    Var var;
}

struct But
{
    Reachable reachable;
}

struct Nested
{
    But but;
}

struct Deeply
{
    Nested nested;
}

struct Some
{
    Deeply deeply;
}

class C
{
    Some some;
}

void f(const C c)
{
    c.some.deeply.nested.but.reachable.var++; // OK (compiled with D2)
}

Notice that the Var sturct is semantically equivalent to an int data member 
(or will be, at least, when opImplicit cast works).

The point is that logical const is still possible, even with transitive 
const, because the global namespace is not const.  There is no way around 
this except with pure functions.  Which I think you agree.

HOWEVER, the point that everyone is arguing is why does logical const make 
pure functions or functional programming impossible?  Clearly, it is ALREADY 
POSSIBLE to have logical const, and clearly, pure functions are possible! 
I'm saying transitive const is mathematically equivalent to logical const 
ALREADY.  Please try and grasp that concept.

-Steve 





More information about the Digitalmars-d mailing list