Logical const

Walter Bright newshound2 at digitalmars.com
Sun Nov 28 17:03:08 PST 2010


Jonathan M Davis wrote:
> On Sunday 28 November 2010 14:02:02 Walter Bright wrote:
>> Jonathan M Davis wrote:
>>> Walter is certainly against logical constness,
>> I don't know of any language that provides logical constness as a feature.
>> I know of no way to make it a statically verifiable attribute. Doing it
>> with runtime checks is arbitrarily complex (imagine having to verify with
>> a runtime check that one didn't change a 1Gb data structure).
>>
>> D has a focus on providing features that promote verifiability of programs.
>> As programs inevitably get more complex, I believe this is crucial (as
>> opposed to the older technique of relying on convention).
> 
> The more I look at your comments on the matter, the more I see that logical 
> constness does not really give you any guarantees in the general case, because 
> it's technically possible to cast it away and ignore it or just make all of your 
> member variables mutable and ignore it.

More than that, nothing at all about the mutable qualifier says that there can 
be only one value assigned to it. Logical constness is not supported.

> But (if nothing else, due to programmers 
> not abusing it that badly) programmers don't usually view it that way. From the 
> programmers point of view, it does provide guarantees, and in practice, I 
> believe that it generally holds.

I'd rather not conflate an unchecked convention with a guarantee.

> But it is true that when you look at it, it's 
> convention that's holding it together (albeit one which is generally followed). 
> So, I do think that D has taken the correct route (though logical constness 
> _can_ be really nice at times), but I'm sure that most programmers aren't going 
> to see it that way (certainly not without a fari bit of convincing), especially 
> when so many of them are used to going without it entirely.

These kinds of things are what make C++ expensive to code in. Expensive in that 
it costs you plenty to hire a competent C++ programmer. Even then, you have no 
guarantee that your coding team is actually following those conventions. Too 
many times, individual coders will find it expedient to "cheat" on them, or 
simply make a mistake. How would you know if they did or not?


> I'm glad to finally understand why you're against logical constness though. I'd 
> never quite understood it before. It's definitely food for thought.

The const/immutable regime in D is unique as far as I know, and is a bit of a 
shock to those coming from the C++ world.


More information about the Digitalmars-d mailing list