Is there any real reason to use "const"?

Ali Çehreli acehreli at yahoo.com
Mon Jan 24 17:47:51 UTC 2022


On 1/24/22 08:23, rempas wrote:

 > I can understand and agree the cases where people will just make
 > mistakes because they are humans

Definitely.

 > or because they are coding at a time
 > they should be sleeping

That's misleading. Making mistakes is an important part of learning. It 
happens all the time. That's why we have processes to follow to protect 
ourselves from ourselves. const, unnecessary (!) curly braces, etc. are 
parts of such protection.

However, const on the function API is also for communication: It tells 
the caller what parameters are not going to be mutated by the function. 
But I've become one of the people who advocate 'in' over 'const' 
especially when compiled with -preview=in:

   https://dlang.org/spec/function.html#in-params

Sweet! 'in' even enables passing rvalues by reference! :)

Ali



More information about the Digitalmars-d mailing list