const and immutable

Tim Verweij tjverweij at gmail.com
Tue Jul 6 07:56:11 PDT 2010


Hey all,

I'm having some trouble understanding the whole const and immutable of D2,
especially since it seems documentation is not consistent (Or I'm missing some
things). I write quite a lot of C++ code btw, so I'm familiar with that.

---

http://www.digitalmars.com/d/2.0/htomodule.html says: D has const as a storage
class, not a type modifier. Hence, just drop any const used as a type modifier:
  void foo(const int *p, char *const q);
becomes:
  void foo(int* p, char* q);

http://www.digitalmars.com/d/2.0/const3.html includes D examples like:
  void foo(const int* x, int* y)

Is the information on the first page not updated for D2?

---

Is the following correct? (this confuses me)

  immutable int somefunc();
means the same thing as
  int somefunc() immutable;
and not the same thing as
  immutable(int) somefunc();
even though the first syntax looks very much like this:
  immutable int x;

---

I think I understand the difference between const and immuable when
considering references and pointers, but how exactly is const different from
immutable in:
const int x; versus immutable int x;
void somefunc(const int x); versus void somefunc(immutable int x);
const(int) somefunc(); versus immutable(int) somefunc();

How does this system interact with in/out/ref etc? Can I for example have
"const ref int somefunc()"?

---

Tim


More information about the Digitalmars-d-learn mailing list