Const system names

bearophile bearophileHUGS at lycos.com
Mon Feb 15 18:13:20 PST 2010


Alternative names for the transitive const regime of D2, the main difference is that now immutable values are the default:

- "immutable" ==> (nothing, it's the default).
- "const" ==> "valview" (a local immutable value view of something).
- mutable variable ==> "var"
- "enum" ==> "const" (or maybe it can be removed, so it becomes immutable, that is no annotation. The link-time optimization of LDC is able to remove unused values from the binary. Ten years from now all D compilers can be able to do this).

This is not compatible with C, but if you try to port C code to D2 with this const regime, the code can't assign new values to variables that lack the "var" attribute, it produce compile errors, so such porting doesn't look bug-prone to me.

So this code:
  immutable int x1 = 1;
  const int x2 = 2;
  int x3 = 3;
  enum int x4 = 4;

Becomes:
  int x1 = 1;
  valview int x2 = 2;
  var int x3 = 3;
  int x4 = 4;

With such immutability by default, thread-local by default, notnull references by default, the language starts to look quite different from C++ ;-)

Bye,
bearophile



More information about the Digitalmars-d mailing list