const, final, scope function parameters
Frank Benoit
keinfarbton at googlemail.com
Mon May 28 04:41:00 PDT 2007
> Would you please post some example. My experience in C, C++, and Java is
> different from yours. I'm not saying you are exactly wrong, I understand
> where you are coming from, especially with class instance references but
> my way of coding has more mutable variables than constants. I think it
> is a matter of program style and I don't mind writing "final Xx x = new
> Xx ();" which I do often in Java.
>
The idea of a save default is, that if you don't care, the compiler
raises an error if you change the value after the initial assignment.
The compiler can only detect a missing "mutable" (or call it 'var'), but
there is no way the compiler can detect a missing "const". And that is
the advantage of const by default.
So i think it is not a matter of "I use more const than non-const" or
vice versa, it is a issue of making mutable things explicitely.
an example i see here:
foreach( m; myCollection ){
C c = m.getC();
c.doA();
c.doB();
C c2 = m.getTheOtherC();
}
m is const, because its live cycle starts and ends with each iteration.
Most of my object references are initialized once. I omit to reuse
reference variables.
Object member variables, that are not initialized in the ctor, are worth
to be marked as mutable, because they are the complicated states in the
object, which need special care.
More information about the Digitalmars-d
mailing list