Why do immutable variables need reference counting?

Dom DiSc dominikus at scherkl.de
Tue Apr 12 10:28:29 UTC 2022


On Monday, 11 April 2022 at 22:10:07 UTC, Ali Çehreli wrote:
> 1) I will not mutate data through this reference. For example, 
> a parameter that is pointer to const achieves that:
>
>   void foo (const(int)[] arr);
>
> 2) This variable is const:
>
>   const i = 42;
>
> Well, there is the confusion: There is no "reference" in the 
> second case at all!
I think this second case should not be allowed. Use

    immutable i = 42;

instead. The meaning is identical, but we could remove the burden 
of two different meanings from const if it is not allowed. const 
should only be allowed in function declarations. A variable must 
be declared either mutable or immutable. It's only functions that 
may guarantee not to modify a parameter or the objects they 
belong to, and so are allowed to work on both mutable and 
immutable objects.



More information about the Digitalmars-d-learn mailing list