[Issue 6695] typeof(this) does not take into account const/immutable attributes inside member functions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 20 05:54:37 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6695


Steven Schveighoffer <schveiguy at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy at yahoo.com
            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 <schveiguy at yahoo.com> 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;
   b1.c();
}

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
immutable(b).

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:
http://www.d-programming-language.org/declaration.html#Typeof

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: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list