Yet yet yet yet another thread about const and immutable.

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Oct 25 09:51:11 PDT 2011


>From stack overflow:

> And since a const variable could actually refer to immutable data, casting away const to alter a variable or allowing for const variables to somehow be altered would be bad, to say the least. So, the language doesn't allow it.

That was the only thing you should have said, instead of the ultra-defensive:

> D does not support logical const using const. Period.

I didn't actually think about the times, when const refers to immutable.
Considering this, it's definitely not a good idea to play with const this way.
Case closed. Thank you for your time.

On Tue, Oct 25, 2011 at 8:39 PM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> On Tuesday, October 25, 2011 09:05 Gor Gyolchanyan wrote:
>> Why isn't it going to? The potential very powerful use of const is
>> restricted because of this little misfeature. Declaring const
>> variables instead of immutable variables has no advantage currently.
>
> As I said, casting away const and modifying a variable is _undefined_ by the
> language. D does not support logical const using const. Period. And there's no
> reason why declaring a value type variable on the stack const should have any
> advantage over declaring it immutable. In neither case is the variable going
> to be modified without subverting the type system, and the compiler is free to
> make whatever optimizations that it can based on that fact. The primary
> differences between const and immutable show themselves when dealing with
> stuff on the heap.
>
> http://stackoverflow.com/questions/4219600/logical-const-in-d
>
> - Jonathan M Davis
>


More information about the Digitalmars-d mailing list