const vs immutable for local variables

Russel Winder russel at russel.org.uk
Thu Nov 18 03:07:18 PST 2010


On Thu, 2010-11-18 at 01:27 -0800, Jonathan M Davis wrote:
[ . . . ]
> enums _must_ known at compile time. immutable variables don't, so they won't 
> actually be compile time constants. If you use immutable, it'll be created at 
> runtime and be less efficient.

I guess I am tainted with the C/C++ prejudice that using enums for
compile time constants was a hack because they didn't have const.  When
const arrived the compiler did all the right things and using enums
became bad practice.  The implication of the above is that DMD does not
transform immutable values to instruction literals when it can, so using
what is now deemed bad practice in C++ is correct idiomatic D?

[ . . . ]
> The real question here is whether there's any real difference in how well 
> unnecessary local variables get optimized out when they're const or immutable. 
> Do they disappear more as const? or as immutable? Or is it the same for both?

I have no idea ;-)  I guess Walter need to comment on this.

[ . . . ]
> > 
> > 	invariant n = 1000000000 ;
> > 	invariant delta = 1.0 / n ;
> > 
> > instead of:
> > 
> > 	immutable n = 1000000000 ;
> > 	immutable delta = 1.0 / n ;

> It used to be that immutable wasn't a keyword. invariant was used. Some folks 
> complained and Walter added immutable. So, immutable is supposed to be used for 
> variables whereas invariant is just for invariants, but it still works to use 
> invariant instead of immutable.

Aha, it wasn't a mistyping, it was a "before a change".  Thanks for
pointing the above out, it explains a lot.


-- 
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/c641e4da/attachment.pgp>


More information about the Digitalmars-d-learn mailing list