Static member functions

Tomek Sowiński just at ask.me
Tue Dec 8 11:09:10 PST 2009


Dnia 08-12-2009 o 19:46:02 Michal Minich <michal.minich at gmail.com>  
napisał(a):

> 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

I think immutable static member functions don't make sense and rightly so.  
On functions immutable is about "this" and there's no "this". Currently  
everything inside an immutable struct/class is tagged with "immutable". I  
say, tag everything but static functions. Let immutable types define them  
like anyone else.


Tomek



More information about the Digitalmars-d mailing list