Steven Schveighoffer <> changed:

           What    |Removed                     |Added
                 CC|                            |
            Version|unspecified                 |D2
            Summary|immutable not inherited by  |typeof(this) does not take
                   |members                     |into account
                   |                            |const/immutable attributes
                   |                            |inside member functions

--- Comment #1 from Steven Schveighoffer <> 2011-09-20 
05:53:59 PDT ---
This is only a problem with the expression typeof(this), the real reference
'this' actually *is* immutable:

simplified example:

import std.stdio;

struct b {
   void c () immutable {
      writeln(typeid(typeof(this))); // b
      auto x = this;
      writeln(typeid(typeof(x))); // immutable(b)

void main()
   immutable b b1;

However, if you tried to change a member of b, you will get a compiler error. 
So the type of the this reference inside c() is not simply b, it really is

However, typeof(this) is a special expression that the compiler replaces with
the actual type of the struct (i.e. struct b).

This is so it can be used in static functions and at a declaration level.  See
the special cases here:

However, I think it is extremely unintuitive to make that happen inside a
member function as well.  I'm not sure if this is an enhancement, it's not
exactly clear from the spec that typeof(this) should still be a special case
*inside* a member function.  It says it should be the same as if it were inside
a member function, it doesn't say *what kind* of member function.

At the very least, this is a doc bug.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to