Is there any real reason to use "const"?

Steven Schveighoffer schveiguy at gmail.com
Mon Jan 24 19:13:23 UTC 2022


On 1/24/22 11:27 AM, rempas wrote:

> Thanks for your time! My question is if you think that it will be very 
> very bad to not include it in my language or not. I'm slowly changing my 
> mind after seeing all these comments and thinking about including it tho...

Some newer languages are gravitating towards const by default.

For example, Swift by default takes parameters as constants. You used to 
be able to take them with the `var` designation, but now you have to 
declare a new variable in the function if you want to have a mutable 
copy. If you declare a variable with `var` and don't mutate it, the 
compiler complains that you really should use `let` (the const equivalent).

The advantages of const to prevent mistakes definitely exist, and most 
people appreciate it. That being said I generally don't use it unless I 
think of it, or I have to for a library to work.

-Steve


More information about the Digitalmars-d mailing list