const as default for variables

Jonathan M Davis via Digitalmars-d digitalmars-d at
Sun Mar 15 01:29:26 PDT 2015

On Saturday, March 14, 2015 13:14:50 Walter Bright via Digitalmars-d wrote:
> I've often thought, as do many others here, that immutability should be the
> default for variables.
> [This is a long term issue. Just thought maybe it's time for a conversation
> about it.]
> Because immutable is transitive, declaring variables as immutable by default
> would be problematic. A more practical way would be to make them const.
> As it is now:
> 1.    int x = 1;       // mutable
> 2.    auto x = 1;      // mutable
> 3.    const x = 1;     // const
> 4.    immutable x = 1; // immutable
> Case (1) is what I'm talking about here. If it is made const, then there are a
> couple ways forward in declaring a mutable variable:
> a) Introduce a new storage class, called 'var' or 'mut'. (Please, no
> bikeshedding on names at the moment. Let's stay on topic.)
> b) Use 'auto' as meaning 'mutable' if the initializer is also mutable. Extend
> 'auto' to allow an optional type,
>      auto T t = initializer;
> There may be some ambiguity issues with 'auto ref', haven't thought it through.
> Once there is a non-default way to declare variables as mutable, a compiler
> switch can be added to change the default to be const. Eventually, the language
> can default to them being const, with a legacy switch to support the mutable
> default.

Given how restricted const in D is and how many things don't work well with
it (e.g. it is a royal pain to make ranges work when const is involved), I
seriously question that this is a good idea. And I agree with deadalnix that
if we were to change the default, immutabale would make more sense.  const
has all of the negatives of immutable without providing many of its
benefits.  Yes, const provides _some_ benefit, but it's pretty limited and
very limiting. So, if we're considering something like this, why not just go
straight to immutable and actually get the full benefits rather than get the
pain with very few benefits?

But really, given how limiting const and immutable are, I seriously question
that making either of them the default is a good idea - _especially_ when
you consider how abysmally they interact with ranges and how big ranges are
for us.

Also, if we're thinking about making a change like this, it would be _way_
more beneficial to do something like make @safe and pure the default for
functions rather than make const the default for variables. At least in that
case we'd be reducing the amount of attribute clutter in the language,
whereas what you're suggesting would probably make code more verbose in
general rather than less (due to having to mark so much as explicitly
mutable), and even if it didn't generally make code any more verbose, it
certainly wouldn't make it _less_ verbose. Making @safe and pure the default
would actually help reduce one of the major issues that we have - attribute
proliferation, whereas this suggestion pushes one of the more limiting
features as the default.

- Jonathan M Davis

More information about the Digitalmars-d mailing list