const(FAQ)

guslay guslay at gmail.com
Sat Mar 29 15:44:36 PDT 2008


Kevin Bealer Wrote:

> guslay Wrote
> ... logical const ...

I still don't see how dividing a const composite object (ie a class) in two separate parts (mutable and immutable) would prevent any optimization or thwart future development.

I can see the optimization benefit of an eventual pure qualifier. With no possible side effects, it would allow crazy functional programming tricks (reordering, parallel dispatch, memoizing).

But const (or invariant) doesn't mean pure. The following does compile, and is bitwise const. It's legal and it's full of side effects.

static int dim = 0;
class Vector
{
     public const int size()
     {
         return ++.dim;
     }

     public const void printElement( int i )
     {
         // print element i to stdout
     }
};

As opposed to pure function, the only "mathematical" guarantee that the const qualifiers gives on a method is that its immutable bits will not be modified. Nothing else. So what is the problem if I say that only half the bits are immutable?

Const/invariant on data enables aggressive optimizations; const/invariant on a method does not.

Or am I missing something?





More information about the Digitalmars-d mailing list