www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static member functions

reply Michal Minich <michal.minich gmail.com> writes:
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
Dec 08 2009
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Tue, 08 Dec 2009 21:46:02 +0300, Michal Minich  =

<michal.minich gmail.com> wrote:

 Discussion with Tomek Sowi=C5=84ski and Steven Schveighoffer moved fro=
m
 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 ha=
ve
 *this* reference.

 I propose changing this rule 2 to: "Immutable *non-static* member
 functions are guaranteed that the object and anything referred to by t=
he
 this reference is immutable." and adding this one: "Immutable static
 member functions are guaranteed that the static variables of object an=
d
 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 membe=
r
 function? Currently there are none.

 Consider this example:

 struct S
 {
     static int x;

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

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

 There is already bugzilla entry: http://d.puremagic.com/issues/
 show_bug.cgi?id=3D3598
--vote; You idea doesn't generalize to other language components. Apart from = static members, there are global variables. Do you suggest that all free= = functions that don't modify global state should also be marked as = immutable? What does it give? AFAIK, immutable member functions can freely modify global state (not to= = be confused with pure functions). As such, they can access non-immutable= = static class function and there is no reason to have this qualifier. OTOH, if you want to protect yourself from (accidental) global state = mutation, you are free to declare your static function as pure: struct X { static int x; static void foo() pure { // x =3D 3; // error } static void bar() { x =3D 3; // okay } } As a result, I don't see any point for introduction static function = qualifiers. This is also inconsistent with ordinary member functions: = const/immutable applies to "this" pointer, passed to these functions as = = hidden parameter. Static functions have no "this" and as such = const/immutable make no sense for them.
Dec 08 2009
parent reply Michal Minich <michal.minich gmail.com> writes:
Hello Denis,

 OTOH, if you want to protect yourself from (accidental) global state
 mutation, you are free to declare your static function as pure:
 
 struct X
 {
 static int x;
 static void foo() pure
 {
 // x = 3; // error
 }
 static void bar()
 {
 x = 3; // okay
 }
 }
I was thinking about providing contract to function, that it does not *modify* state, something as half-pure. Anyway, this would be hard to define correctly.
Dec 09 2009
parent =?ISO-8859-2?Q?=22J=E9r=F4me_M=2E_Berger=22?= <jeberger free.fr> writes:
Michal Minich wrote:
 Hello Denis,
=20
 OTOH, if you want to protect yourself from (accidental) global state
 mutation, you are free to declare your static function as pure:

 struct X
 {
 static int x;
 static void foo() pure
 {
 // x =3D 3; // error
 }
 static void bar()
 {
 x =3D 3; // okay
 }
 }
=20 I was thinking about providing contract to function, that it does not=20 *modify* state, something as half-pure. Anyway, this would be hard to=20 define correctly. =20
That's what gcc calls "__attribute__((pure))" (they use=20 "__attribute__((const))" for what D calls "pure") Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
Dec 09 2009
prev sibling next sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 08-12-2009 o 19:46:02 Michal Minich <michal.minich gmail.com>  =

napisa=B3(a):

 Discussion with Tomek Sowi=F1ski 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 ha=
ve
 *this* reference.

 I propose changing this rule 2 to: "Immutable *non-static* member
 functions are guaranteed that the object and anything referred to by t=
he
 this reference is immutable." and adding this one: "Immutable static
 member functions are guaranteed that the static variables of object an=
d
 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 membe=
r
 function? Currently there are none.

 Consider this example:

 struct S
 {
     static int x;

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

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

 There is already bugzilla entry: http://d.puremagic.com/issues/
 show_bug.cgi?id=3D3598
I think immutable static member functions don't make sense and rightly s= o. = 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 the= m = like anyone else. Tomek
Dec 08 2009
parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 08-12-2009 o 20:09:10 Tomek Sowi=F1ski <just ask.me> napisa=B3(a):

 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
For clarity, I'm not commenting any of Michal's examples, I'm talking = about this: immutable struct Strukt { static void funkcja() { } // now doesn't compile }
Dec 08 2009
parent Michal Minich <michal.minich gmail.com> writes:
Hello Tomek,

 Dnia 08-12-2009 o 20:09:10 Tomek Sowiński <just ask.me> napisał(a):
 
 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.
You should probably specify it in the bug report http://d.puremagic.com/issues/show_bug.cgi?id=3598 This resolution is, at least for me, not obvious.
 Let immutable  types define them like anyone else.
 
 Tomek
 
For clarity, I'm not commenting any of Michal's examples, I'm talking about this: immutable struct Strukt { static void funkcja() { } // now doesn't compile }
We both agree it is a bug. You want to fix it not marking static function as immutable when the they are inside immutable type. I was trying to fix it by giving some meaning to static immutable functions, which seems to be not so good idea…
Dec 09 2009
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 08 Dec 2009 13:46:02 -0500, Michal Minich  
<michal.minich gmail.com> 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
Dec 08 2009