thoughts on immutability in D

Marco Leise Marco.Leise at gmx.de
Wed Sep 28 23:42:40 PDT 2011


Am 24.09.2011, 07:28 Uhr, schrieb Mehrdad <wfunction at hotmail.com>:

> On 9/23/2011 8:11 PM, Jonathan M Davis wrote:
>> On Friday, September 23, 2011 19:42:24 Walter Bright wrote:
>>> On 9/23/2011 6:55 PM, Jonathan M Davis wrote:
>>>> True, but it doesn't just happen. You have to choose to make a  
>>>> variable
>>>> mutable.
>>> Which you *must* do for logical const. And, of course, anything beyond  
>>> the
>>> first level is not const at all, and there's NO WAY to say it is const.
>>>
>>>> _Some_ effort must be put in to circumvent const. Until you do that,
>>>> const protects it from be changed.
>>> There's no way to detect who or what is changing it.
>>>
>>>> By no means am I claiming that C++'s const is without flaws. I'm just
>>>> claiming that it's not worthless, and you seem to be claiming that  
>>>> it's
>>>> worthless.
>>> I think it is worthless because:
>>>
>>> 1. a number of C++ programmers I've talked to who relied on being able  
>>> to
>>> change const objects and insisted that was a feature.
>>>
>>> 2. the pervasive misconceptions about what C++ const guarantees and  
>>> what it
>>> doesn't, even among C++ committee members.
>>>
>>> 3. the complete uselessness C++ const has for multithreaded  
>>> programming.
>>>
>>> 4. the pervasiveness of the Double Checked Locking Bug, which is one  
>>> of the
>>> consequences of (3).
>>>
>>> 5. it's head-const only. Anything beyond one level of indirection is
>>> completely const-free. Only trivial data structures are one level.
>> Well, I grant you that all of those are issues, but I don't think that  
>> they
>> makes C++'s const worthless. Const has caught bugs in my code for me. I  
>> have
>> found it to be useful in the projects that I've worked on. So, we're  
>> obviously
>> just going to have to agree to disagree on this, and I think that a lot  
>> of C++
>> programmers are going to disagree with you (including many who  
>> understand
>> exactly what const does and doesn't guarantee).
>>
>> - Jonathan M Davis
> +1 gotta agree 100% with Jon here. (Hey, is that the first time?! :P) I  
> wouldn't believe anyone for 1 second if he said he's used const in his  
> code and it never caught an error for him (or if it introduced more than  
> it caught).

Of course it does, because these bugs were logical bugs, an area where  
logical const is a perfect match. (You don't need guarantees, just the  
semantic analysis at compile time.)


More information about the Digitalmars-d mailing list