Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 2 08:37:51 PDT 2008


> pure is a tool for multiprogramming.  This allows all the fun stuff that 
> Walter is always talking about.  pure can co-exist with logical const and 
> logical invariant, because if it couldn't, then pure couldn't exist in 
> today's version of const.

Let me expand on this point, because I may not have explained this correctly 
and as a result, a lot of you are confused at what I'm talking about.

pure functions require transitive invariance.  But this does not mean that 
an invariant object must be transitively invariant for pure to work.  For a 
logically invariant object, a pure function will simply not be able to use 
the mutable portion of that object.

Put another way:  pure requires transitive invariance, but making the 
keywords invariant and const mean 'transitive' is not *required* for this to 
happen.  What is required is a well defined const system, where the compiler 
can tell what is const and what is not const.

The problem with C++'s const and pure is that by default const is not 
transitive.  Therefore, the compiler has no way of knowing if the developer 
needs data referenced by const members to be const or not.  If D had this 
kind of system, then the compiler would have to restrict pure functions to 
only interact with local value members of a class that did not reference 
other data.  With const being transitive by default, the compiler can make 
more assumptions, and have greater assurance that pure has no side effects, 
but allowing a developer to specify that a piece of a class is not part of 
the class state does NOT prevent useful pure functions, and does NOT prevent 
the compiler from enforcing const or pure.  i.e. logical const does not 
invalidate pure functions and does not prevent multiprogramming as Walter 
has suggested.

To summarize:  Yes I understand pure functions require transitive 
invariance.  Yes I understand what logical invariance means.  I have shown 
that pure functions are still possible alongside logical invariance, as long 
as the pure function does not use the mutable portion of the class (which is 
technically not part of the class state anyways).  I have also shown that 
logical invariance is possible with today's transitive const system, proving 
that transitive const is semantically equivalent to logical const.

-Steve 





More information about the Digitalmars-d mailing list