www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - More on immutable

reply bearophile <bearophileHUGS lycos.com> writes:
I am trying to learn more about the usage of immutable. This is a little D2
program:


immutable class S {
    static int x;
    static this() {
        S.x = 10;
    }
}
void main() {}


The compiler outputs:
test.d(3): Error: function test2.S._staticCtor1 without 'this' cannot be
const/immutable

Can you tell me what's wrong or how I can fix it?

Bye and thank you,
bearophile
Mar 08 2010
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 08 Mar 2010 09:08:26 -0500, bearophile <bearophileHUGS lycos.com>  
wrote:

 I am trying to learn more about the usage of immutable. This is a little  
 D2 program:


 immutable class S {
     static int x;
     static this() {
         S.x = 10;
     }
 }
 void main() {}


 The compiler outputs:
 test.d(3): Error: function test2.S._staticCtor1 without 'this' cannot be  
 const/immutable

 Can you tell me what's wrong or how I can fix it?
There was a discussion on D.learn in December of last lear that resulted in this bug being filed: http://d.puremagic.com/issues/show_bug.cgi?id=3598 Note that even if static functions were allowed in immutable classes/structs, the static field members will still be tagged as immutable, so your example shouldn't compile. -Steve
Mar 08 2010