const and immutable

Simen kjaeraas simen.kjaras at gmail.com
Tue Jul 6 08:11:55 PDT 2010


Tim Verweij <tjverweij at gmail.com> wrote:

> 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?

That seems correct.


> 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;

Yes. immutable always applies to the whole type, which in the function
case would be int function().


> 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();

It isn't. For non-reference types, immutable and const are
indistinguishable.

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

Absolutely.

-- 
Simen


More information about the Digitalmars-d-learn mailing list