mutable, const, immutable guidelines

qznc qznc at web.de
Tue Oct 8 15:03:30 PDT 2013


On Wednesday, 2 October 2013 at 13:09:34 UTC, Daniel Davidson 
wrote:
> 1. If a variable is never mutated, make it const, not immutable.
> 2. Make the parameter reference to immutable if that is how you 
> will use it anyway. It is fine to ask a favor from the caller.
> ...

I think guideline 1 should be about "arguments" not "variables". 
Functions should take const arguments, so they can be called with 
mutable and immutable values.

I would rephrase the second guideline as: "Never dup or idup an 
argument to make it mutable or immutable, but require the caller 
to do this (might be able to avoid it)".

For more guidelines:

3. Return value, which are freshly created, should never be 
const. They should be mutable or immutable.

Basically, I cannot come up with a case, where const would be 
preferable to mutable and immutable. Maybe someone is able to 
find a good example? Of course, this does not hold for returned 
values, which are retrieved from somewhere (array,container,etc). 
In this case, I have no general advice.

4. Data structures should not restrict themselves to be mutable, 
const, or immutable.

This is a noble goal, which in reality is probably either trivial 
or impossible. So I am not sure, if it is worthy to be called a 
guideline.


More information about the Digitalmars-d-learn mailing list