Logical const

Max Samukha spambox at d-coding.com
Tue Nov 30 09:05:47 PST 2010


On 11/30/2010 05:39 PM, Andrei Alexandrescu wrote:
> On 11/30/10 5:25 AM, Max Samukha wrote:
>> On 11/30/2010 02:35 AM, Walter Bright wrote:
>>> Fawzi Mohamed wrote:
>>>> logical const is useful for lazy functions and memoization, and if
>>>> implemented correctly it is perfectly safe.
>>>> As I said in an older discussions, to have it with the current system
>>>> all that is needed is some guarantees that the compiler will not
>>>> disallow "unsafe" changes (by moving to read only memory for
>>>> example)in some cases.
>>>> For example casted mutable types, so that casting to mutable works.
>>>
>>> D allows escape from the type system, but the programmer who does that
>>> loses the guarantees, and it's up to him to ensure that the result
>>> works.
>>>
>>> String literals, for example, are going to often wind up in read only
>>> memory.
>>
>> The problem is that logical const has many perfectly valid use cases.
>> You cannot simply tell people: "Don't use it. It is a fraud". They will
>> still be using casts or not using D. As casting away const is undefined
>> behavior in D, the outcome will be every second non-trivial D program
>> relying on undefined behavior.
>
> I'm not seeing half of non-trivial C++ programs using mutable.
>
> Andrei

That was a hyperbole. But logical const is obviously not some obscure 
corner case. Objects with reference counters, proxy objects, objects 
keeping access statistics (e.g. for debugging purposes), objects using 
logger objects, etc - all require logical const. To corroborate, Qt 
sources have over 600 'mutable' field declarations.


More information about the Digitalmars-d mailing list