Is there any real reason to use "const"?

vit vit at vit.vit
Mon Jan 24 11:51:55 UTC 2022


On Monday, 24 January 2022 at 10:49:36 UTC, Dom DiSc wrote:
> 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.
>>
> ...
> 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.

`const` variable are necessary:
- in generic code when you don't know if variable is mutable or 
`immutable`.
- if you initialize variable from `const` function parameter or 
other `const` variables
- when you don't want shared variable (`immutable` is implicitly 
shared, `const` is not).
- ...




More information about the Digitalmars-d mailing list