thoughts on immutability in D

Timon Gehr timon.gehr at gmx.ch
Sat Sep 24 10:07:04 PDT 2011


On 09/24/2011 01:34 AM, Mehrdad wrote:
> On 9/23/2011 4:13 PM, Jonathan M Davis wrote:
>> On Friday, September 23, 2011 15:58 Andrej Mitrovic wrote:
>>> On 9/24/11, Peter Alexander<peter.alexander.au at gmail.com> wrote:
>>>> On 23/09/11 6:48 AM, Walter Bright wrote:
>>>>> On 9/22/2011 4:39 AM, bearophile wrote:
>>>>>> Walter:
>>>>> "logical const" in C++ is faith-based programming.
>>>> I think you're exaggerating it's uselessness.
>>>>
>>>> It's faith-based as much as 'walking down the street assuming that the
>>>> next person won't stab you' is faith-based. There's no guarantee, but
>>>> 99% of the time your "faith" is well placed.
>>> So then don't use const if u need caching and use convention if your
>>> faith is well placed!
>> const in C++ really does help to catch bugs. It's definitely better than
>> nothing. I do _not_ think that it is worthless like Walter at least
>> gives the
>> impression that he thinks.
>>
>> Now, I _do_ think that on the whole, D's const is better. The
>> transitiveness
>> of const is a major improvement IMHO, and the caching thing, while
>> annoying,
>> isn't that big a loss in my experience. I expect that with Peter's gaming
>> background, it's much more of an issue for him. I think that it's at
>> least
>> theoretically possible to get a caching mechanism to work with const,
>> which
>> would overcome that portion of the logical const problem, but the
>> question is
>> how best to do it and whether the added complication is worth the cost.
>> Certainly, it's the sort of thing that we should look into for D3 when
>> we get
>> that far. But for now, on the whole, D's const is a definite
>> improvement over
>> C++'s const, and you just don't use const in D in quite as many places
>> as you
>> would have in C++.
>>
>> But while C++'s const is not as good as D's const, it's still very
>> worthwhile
>> IMHO.
>>
>> - Jonathan M Davis
> So far, the 5% of the code it breaks has made it 95% worthless for me,
> though I'm not sure about others (would love to hear otherwise)...

If you are trying to use const in the same way as C++ const and you are 
not using immutable in combination with const then you probably don't 
use the feature properly. I don't know for sure if this is the case 
though. Is it?


More information about the Digitalmars-d mailing list