Why can't function expecting immutable arg take mutable input?

Shriramana Sharma via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Oct 16 19:02:57 PDT 2015


Ali Çehreli wrote:

http://ddili.org/ders/d.en/const_and_immutable.html#ix_const_and_immutable.parameter,
%20const%20vs.%20immutable

Hi Ali – I take this chance to personally thank you sincerely for your book 
which provides much-needed hand-holding in my baby D-steps. I did read that 
chapter already and IMO you have given clear instructions as to when to use 
const and when immutable.

My question was however to the root of the issue, as to *why* the compiler 
cannot consider mutable as immutable just like in C/C++ any non-const can be 
taken as const.

It would seem that the answer is one related to optimization. Obviously, 
labeling an argument as immutable can be done only if we are sure that we 
will have to process only immutable input, thereby paving the opportunity 
for the compiler to optimize some memory access or allocation or such – I'm 
not much clear beyond that but that's enough for me now...

-- 
Shriramana Sharma, Penguin #395953


More information about the Digitalmars-d-learn mailing list