const vs immutable for local variables

div0 div0 at sourceforge.net
Thu Nov 18 10:07:46 PST 2010


On 18/11/2010 08:50, Russel Winder wrote:
> On Wed, 2010-11-17 at 23:21 -0800, Jonathan M Davis wrote:
> [ . . . ]
>> Well. yes. enums are definitely tha case for compile time constants. The question
>> is for runtime. And why would you suggest immutable over const for runtime?
>
> Why use enums rather than immutable for values that are known at compile
> time?
>
> immutable is really immutable whereas const implies that there is the
> possibility of change -- at least that is how I read the documentation
> and TDPL.
>
> [ . . . ]
>> I really don't see any reason why const vs immutable would make any difference
>> for a local variable except insofar as a function takes an immutable argument
>> rather than a const one. I would think that both would be optimized identically,
>> but I don't know.
>
> I am a fan of single assignment so I put immutable on all my variables
> except for loop control variables and accumulators.  I haven't yet seen
> a need for const.
>
> Interesting, and possibly not irrelevant, side note:  In a moment of
> complete stupidity I spelled immutable as invariant so had code like:
>
> 	invariant n = 1000000000 ;
> 	invariant delta = 1.0 / n ;
>
> instead of:
>
> 	immutable n = 1000000000 ;
> 	immutable delta = 1.0 / n ;
>
> and it all worked just fine.  I have no idea how or why, but it did!
>

invariant is the old deprecated name for immutable.
It'll go away eventually.

-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk


More information about the Digitalmars-d-learn mailing list