[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 11:42:46 PDT 2011


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



--- Comment #8 from Steven Schveighoffer <schveiguy at yahoo.com> 2011-09-20 11:42:19 PDT ---
(In reply to comment #7)
> I agree that maybe this is not a bug, but I don't agree with the explanation
> ... 
> 
> Documentation says "Both immutable and const are transitive, which means that
> any data reachable through an immutable reference is also immutable, and
> likewise for const."

Note the key element here is *data*, not *functions*.

> So in this example:

[snip]

> From the documentation, both
>   typeid(typeof(a1.b1.d))
> and
>   typeid(typeof(a1.b1.f))
> should be either mutable or immutable, right ?

typeid(typeof(a1.b1.d)) is a method, not a piece of data.  In fact, when you
say typeof(a1.b1.d), you might as well have written typeof(b.d).

There is a confusing concept to grasp, and it's made harder because of the
terminology.  An immutable function is not actually immutable.  An immutable
function is a member function that *can be called* on an immutable instance of
the struct/class.  The immutable modifier only affects the 'this' pointer, even
though it seems like it's applied to the entire function.

Remember that all member functions have a hidden this pointer?  If we were
forced to write them out, then this:

struct S
{
   void foo () immutable {}
   void foo2()     const {}
   void foo3()           {}
}

becomes this:

struct S
{
   void foo (ref immutable(S) this) {}
   void foo2(ref     const(S) this) {}
   void foo3(ref           S  this) {}
}

Now you can see why its clear you cannot call for instance foo with a
non-immutable instance of S.  The reason the modifiers are applied to the
function is because there's no parameter for them to cling to -- the parameter
is hidden.

If you want to discuss this more, I think the d.learn newsgroup is probably
best, or you can email me directly.  I'd be happy to answer any questions on
it.

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