Static member functions

Michal Minich michal.minich at gmail.com
Tue Dec 8 10:46:02 PST 2009


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.

I propose changing this rule 2 to: "Immutable *non-static* member
functions are guaranteed that the object and anything referred to by the
this reference is immutable." and adding this one: "Immutable static
member functions are guaranteed that the static variables of object and
anything referred to by these variables is immutable"

And I'm asking if this is reasonable, useful, implementable and/or 
desired - Or how should be defined semantics of static immutable member 
function? Currently there are none.

Consider this example:

struct S
{
    static int x;

    static void foo () immutable
    {
       x = 3; // should be error, because immutable static member
function cannot change mutable static data members.
    }

    static void bar () 
    {
       x = 3; // ok
    }
}

There is already bugzilla entry: http://d.puremagic.com/issues/
show_bug.cgi?id=3598



More information about the Digitalmars-d mailing list