const vs immutable for local variables

Jonathan M Davis jmdavisProg at gmx.com
Wed Nov 17 20:41:33 PST 2010


In C++, I tend to declare all local variables const when I know that they aren't 
going to need to be altered. I'd like to something similar in D. However, D has 
both const and immutable. I can see clear differences in how const and immutable 
work with regards to function parameters and member variables, but it's not as 
clear with regards to const and immutable.

So, the question is: what are the advantages of one over the other? Specifically, 
my concern is how likely compiler optimizations are. Does using immutable make 
compiler optimizations more likely? Or would const do just as well if not 
better? Or is dmd smart enough that it really doesn't matter if you use const or 
immutable on local variables which never change?

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list