digitalmars.D.learn - static class instances not allowed?
- Eric (15/15) Jun 11 2013 The following code does not compile:
- bearophile (10/23) Jun 11 2013 C# compilers present bugs with a standard number, like:
- Eric (4/16) Jun 11 2013 I'm not sure what you are saying. Are you implying that not
- bearophile (4/11) Jun 11 2013 The gentle Simen Kjaeraas has just created an enhancement request:
- Steven Schveighoffer (16/20) Jun 11 2013 These can be solved with a static ctor. Essentially, any static
- Eric (5/28) Jun 11 2013 Great. This solved my problem.
The following code does not compile: class Foo { int x; } class Bar { static Foo f = new Foo(); } // compiler error static Foo g = new Foo(); // compiler error void main() {} (dmd7) desk3:~/tp/d_test2/dlib>dmd T.d T.d(4): Error: variable T.Bar.f is mutable. Only const or immutable class thread local variable are allowed, not T.Foo T.d(5): Error: variable T.g is mutable. Only const or immutable class thread local variable are allowed, not T.Foo Why aren't static class instances allowed? Is there a work-around, or alternative approach to this? Thanks, Eric
Jun 11 2013
Eric:The following code does not compile: class Foo { int x; } class Bar { static Foo f = new Foo(); } // compiler error static Foo g = new Foo(); // compiler error void main() {} (dmd7) desk3:~/tp/d_test2/dlib>dmd T.d T.d(4): Error: variable T.Bar.f is mutable. Only const or immutable class thread local variable are allowed, not T.Foo T.d(5): Error: variable T.g is mutable. Only const or immutable class thread local variable are allowed, not T.Foo Why aren't static class instances allowed? Is there a work-around, or alternative approach to this?myprog.cs(7,60): error CS1525: ....... This is useful because you can then write an explanation page for each of those bugs, like CS1525: http://msdn.microsoft.com/en-gb/library/3hdyz4dw%28v=vs.80%29.aspx In such pages you can explain why Only const or immutable class thread local variable are allowed. Bye, bearophile
Jun 11 2013
I'm not sure what you are saying. Are you implying that not allowing static class instances is a compiler bug? -EricWhy aren't static class instances allowed? Is there a work-around, or alternative approach to this?myprog.cs(7,60): error CS1525: ....... This is useful because you can then write an explanation page for each of those bugs, like CS1525: http://msdn.microsoft.com/en-gb/library/3hdyz4dw%28v=vs.80%29.aspx In such pages you can explain why Only const or immutable class thread local variable are allowed. Bye, bearophile
Jun 11 2013
myprog.cs(7,60): error CS1525: ....... This is useful because you can then write an explanation page for each of those bugs, like CS1525: http://msdn.microsoft.com/en-gb/library/3hdyz4dw%28v=vs.80%29.aspx In such pages you can explain why Only const or immutable class thread local variable are allowed.The gentle Simen Kjaeraas has just created an enhancement request: http://d.puremagic.com/issues/show_bug.cgi?id=10335 Bye, bearophile
Jun 11 2013
On Tue, 11 Jun 2013 10:04:21 -0400, Eric <eric makechip.com> wrote:The following code does not compile: class Foo { int x; } class Bar { static Foo f = new Foo(); } // compiler error static Foo g = new Foo(); // compiler errorThese can be solved with a static ctor. Essentially, any static initializers must be evaluatable at compile-time. I know that in the most recent compiler classes have entered this realm, but I don't know the conditions on when they can be used. The accepted way: class Bar { static Foo f; static this() { f = new Foo(); } } static Foo g; static this() { g = new Foo(); } -Steve
Jun 11 2013
On Tuesday, 11 June 2013 at 16:09:39 UTC, Steven Schveighoffer wrote:On Tue, 11 Jun 2013 10:04:21 -0400, Eric <eric makechip.com> wrote:Great. This solved my problem. Many thanks. -EricThe following code does not compile: class Foo { int x; } class Bar { static Foo f = new Foo(); } // compiler error static Foo g = new Foo(); // compiler errorThese can be solved with a static ctor. Essentially, any static initializers must be evaluatable at compile-time. I know that in the most recent compiler classes have entered this realm, but I don't know the conditions on when they can be used. The accepted way: class Bar { static Foo f; static this() { f = new Foo(); } } static Foo g; static this() { g = new Foo(); }-Steve
Jun 11 2013