const vs immutable for local variables

Russel Winder russel at russel.org.uk
Thu Nov 18 00:50:58 PST 2010


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!

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at russel.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20101118/e4ec6b4d/attachment.pgp>


More information about the Digitalmars-d-learn mailing list