Is there any real reason to use "const"?

Dom DiSc dominikus at
Mon Jan 24 10:49:36 UTC 2022

On Monday, 24 January 2022 at 10:23:14 UTC, rempas wrote:
> The problem you described is why most language use a "string" 
> type that is immutable (in case you point to a string literal) 
> but they also have a way to allow you to modify a "string" type 
> in case you have allocated memory. So it's always up to you.
You need const for functions that need to be able to take a 
mutable or immutable variable as parameter. And as you don't know 
which it is, you have to either propagate the constness or make a 
mutable copy if you want to call a function (depending if the 
called function has its parameter declared const or not). So 
better to declare const wherever that is possible, to _avoid_ 
making useless copies.
I would NEVER EVER declare a variable const. It's always either 
mutable or immutable. I would go as far as forbidding to declare 
variables const, it's just confusing and useless. const is only 
for parameters.

More information about the Digitalmars-d mailing list