digitalmars.D.learn - immutable and static this()
- teo (12/12) Mar 21 2011 I cannot initialize immutable class members inside a static this()
- Simen kjaeraas (6/18) Mar 21 2011 Non-static class members require a this pointer, and thus cannot be
- Jonathan M Davis (22/45) Mar 21 2011 No. Error message is essentially correct, just confusing. In this case, ...
- teo (2/21) Mar 21 2011 Yes, that is the message. Thanks for the hint.
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
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
On Mon, 21 Mar 2011 22:27:17 +0100, teo <teo.ubuntu yahoo.com> wrote: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 DavisI 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.
Mar 21 2011
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:Yes, that is the message. Thanks for the hint.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.
Mar 21 2011