Making RCSlice and DIP74 work with const and immutable

Atila Neves via Digitalmars-d digitalmars-d at
Tue Mar 3 05:08:14 PST 2015

On Sunday, 1 March 2015 at 15:49:12 UTC, Jakob Ovrum wrote:
> On Sunday, 1 March 2015 at 15:40:06 UTC, Atila Neves wrote:
>> I've lost count now of how many times I've had to downgrade to 
>> auto despite always wanting immutable or const. This doesn't 
>> work:
>> auto reg = regex(`(foo)`);
>> const match = "foo".matchAll(reg);
>> writeln(match.captures); //oops, captures isn't const
>> It should, but it doesn't. Maxime talked about it here as well:
>> Atila
> `match.captures` is a range; it's only natural for a range to 
> have mutable state to be iterable.
> D's const is a bridge between immutable and mutable. const has 
> to be transitive because immutable is transitive. Don't use it 
> as if it was C++ const: there's no logical const in D, and if 
> there ever will be, it can't use the same `const` type 
> qualifier.

I know why it doesn't work. I know the reasons that go into the 
design decision. const has to be the way it is or else 
thread-safety goes out of the window. I'm not talking about 
logical const; I'm talking about making as many things const and 
immutable as I can to reduce the number of things I have to keep 
in my mind at any given time.

const foo = getFoo(); //no need to reason about foo anymore, it 
won't change
auto bar = getBar(); //oh-oh, now the next 50 lines can do 
whatever they want with this

I shouldn't have to make captures const if all I want to do is 
inspect the 3rd value. And yet I do. That was just one example, 
like I said I've lost count of how many times I've changed const 
to auto in my code because it wouldn't compile otherwise. I don't 
go as far as Manu as use mutable everywhere, just where I have 
to. And I'm always annoyed when it's the case.


More information about the Digitalmars-d mailing list