On Tue, 08 Dec 2009 13:46:02 -0500, Michal Minich
<[email protected]> wrote:
Discussion with Tomek Sowiński and Steven Schveighoffer moved from
digitalmars.D.learn:
Currently it is impossible to have static member function in struct or
class; this does not compile:
struct S2
{
static void foo () immutable { }
}
Error: function main.S.foo without 'this' cannot be const/immutable
The problem I see is in definition of immutable member function:
from D specs: "Immutable member functions are guaranteed that the
object and anything referred to by the this reference is immutable." --
I think this rule is wrong, because it mentions *this* and at the same
time it applies to static member functions, which obviously doesn't have
*this* reference.
As I said in the other thread, immutable doesn't temporarily transform
mutable data to immutable, it is only callable when *all* the data is
immutable. With an instance, this means you can't call an immutable
member unless the object reference is immutable. With static functions,
there's no way to "cast" the static data to immutable. Either static data
members are immutable or not, they cannot be cast later. So the only time
a static function could be called "immutable" is when all the static data
is immutable. But there are no differences between such a function and a
normal static function where all the static data is immutable -- neither
can change any static data!
The only reason for immutable functions is because the this pointer is
hidden, so there's no other place to put the immutable tag. The
*function* is not what the immutable tag is applying to, all functions are
technically immutable because the code does not change. With no 'this'
pointer, the immutable tag means nothing.
BTW, that bug entry is *not* related to what you are saying, the bug
identifies a deficiency in that declaring a whole struct as immutable
tries to illegally declare static functions as immutable as well. I agree
that this is a bug and should be fixed.
-Steve