www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - immutable and static this()

reply teo <teo.ubuntu yahoo.com> writes:
I cannot initialize immutable class members inside a static this() 
constructor. Is there any reason for that?

Example:
class Test
{
    public immutable(int) x;
    static this()
    {
        x = 1; // Error: variable Test.x can only initialize const x 
inside constructor
    }
}
Mar 21 2011
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 21 Mar 2011 22:27:17 +0100, teo <teo.ubuntu yahoo.com> wrote:

 I cannot initialize immutable class members inside a static this()
 constructor. Is there any reason for that?

 Example:
 class Test
 {
     public immutable(int) x;
     static this()
     {
         x = 1; // Error: variable Test.x can only initialize const x
 inside constructor
     }
 }
Non-static class members require a this pointer, and thus cannot be initialized in a static constructor. However, if that is the error message you get, it is clearly misleading. -- Simen
Mar 21 2011
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
 On Mon, 21 Mar 2011 22:27:17 +0100, teo <teo.ubuntu yahoo.com> wrote:
 I cannot initialize immutable class members inside a static this()
 constructor. Is there any reason for that?
 
 Example:
 class Test
 {
 
     public immutable(int) x;
     static this()
     {
     
         x = 1; // Error: variable Test.x can only initialize const x
 
 inside constructor
 
     }
 
 }
Non-static class members require a this pointer, and thus cannot be initialized in a static constructor. However, if that is the error message you get, it is clearly misleading.
No. Error message is essentially correct, just confusing. In this case, x is a member variable, _not_ a class/static variable. So, it must either be directly initialized public immutable int x = 1; or in a constructor (and in this case the constructor would have to be immutable). public immutable int x; this() immutable { int x = 1; } static this() in a class is for initializing class/static variables only, so if you did public static immutable int x; static this() { int x = 1; } that would work. The fact that it is immutable has nothing to do with whether it is a member variable or a class/static variable, so it has no effect on whether a normal or static constructor is used. - Jonathan M Davis
Mar 21 2011
prev sibling parent teo <teo.ubuntu yahoo.com> writes:
On Mon, 21 Mar 2011 22:27:53 +0100, Simen kjaeraas wrote:

 On Mon, 21 Mar 2011 22:27:17 +0100, teo <teo.ubuntu yahoo.com> wrote:
 
 I cannot initialize immutable class members inside a static this()
 constructor. Is there any reason for that?

 Example:
 class Test
 {
     public immutable(int) x;
     static this()
     {
         x = 1; // Error: variable Test.x can only initialize const x
 inside constructor
     }
 }
Non-static class members require a this pointer, and thus cannot be initialized in a static constructor. However, if that is the error message you get, it is clearly misleading.
Yes, that is the message. Thanks for the hint.
Mar 21 2011