Transitive const sucks

Steven Schveighoffer schveiguy at yahoo.com
Wed Sep 12 06:55:48 PDT 2007


"Walter Bright" <newshound1 at digitalmars.com> wrote in message 
news:fc837t$29ni$1 at digitalmars.com...
> Sean Kelly wrote:
>> But for better or worse, D const has a different aim. The goal is largely 
>> to produce a system which allows for compiler optimization rather than to 
>> enforce some sort of logical restrictions on behavior.
>
> This is not the primary goal (it is a side effect of the primary goals). 
> The goals are:
>
> 1) Make functional style programming possible, which will become extremely 
> important as people will start using all those lovely cores.

How does making const transitive allow for functional programming?  From 
what I understand (and that's not much) about functional programming, it's 
programming without side effects.  Consider:

int x;

class X
{
  const int void f()
  {
    x += 5;
    return x;
  }
}

int main()
{
  const X x1 = new X;
  const X x2 = new X;
  int y = x1.f() * x2.f();
}

Although X.f() is const, it has modified global data, so X.f() has side 
effects.  Yet both x1 and x2 are transitive-const, are they not?

Therefore, the compiler STILL cannot make any optimizations based on the 
fact that x1 and x2 are const.

I think in order to allow functional programming, you need to introduce a 
new type of const, for example fconst.  If a function is declared fconst, it 
is not allowed to change any data that is not local to the function, or call 
a non-fconst function, or to read non-fconst data.  If a piece of data is 
declared fconst, it cannot be changed.

I don't think you can make this restriction with const in general as I think 
it will severely limit current programming styles (not everyone wants to use 
functional programming, or else we'd all be using scheme).

> 2) Provide compiler enforced semantic guarantees, which improves the 
> specification of interfaces between disparate parts of code.

As I showed above, this is not possible with simply transitive const.

>
> 3) Be able to treat invariant references as value types. This, for 
> example, makes manipulating strings as easy as manipulating ints.

I don't understand this, perhaps someone can explain it further.  An example 
of how this helps would be good.

> 4) Makes COW programming checkable and enforceable. (COW programming is an 
> important part of many styles of programming.) Without transitive const, 
> COW is overly reliant on programmer discipline.

All I can find on the net about COW programming is the COW programming 
language.  I'm sure this isn't what you meant :)  can you please explain 
this further?

-Steve 





More information about the Digitalmars-d mailing list