digitalmars.D.learn - How do I create a module-local immutable class object?
- Andrej Mitrovic (12/12) Sep 09 2011 I need to have an object which is initialized only once, so I thought
- bearophile (11/23) Sep 09 2011 private class Foo {}
- Andrej Mitrovic (2/7) Sep 09 2011 Oh right, that's the syntax. Thanks!
- Jonathan M Davis (3/29) Sep 09 2011 But make the constructor shared. Otherwise, it gets initialized once per...
- Timon Gehr (2/31) Sep 09 2011 Shouldn't the compiler catch this?
- Jonathan M Davis (5/43) Sep 09 2011 It should, but it doesn't.
- Andrej Mitrovic (18/18) Sep 09 2011 So much for that idea, immutable breaks property functions. Take a look:
- David Nadlinger (3/21) Sep 09 2011 Did you mark the getter const?
- Timon Gehr (10/19) Sep 09 2011 class Foo
- Andrej Mitrovic (2/4) Sep 09 2011 Good call!
- Andrej Mitrovic (15/15) Sep 09 2011 For crying out loud, shared fails too:
- Jonathan M Davis (2/20) Sep 09 2011 I meant to make the static constructor shared.
- Andrej Mitrovic (2/2) Sep 09 2011 Ok so this is much more involved than I thought. I need to re-read
I need to have an object which is initialized only once, so I thought I could use immutable for that. But I can't do this: private class Foo {} immutable Foo foo; static this() { foo = new Foo; } void main() {} And I can't new the object when it's declared. Even if CTFE could new objects, it wouldn't be possible because the ctor depends on API calls.
Sep 09 2011
Andrej Mitrovic:I need to have an object which is initialized only once, so I thought I could use immutable for that. But I can't do this: private class Foo {} immutable Foo foo; static this() { foo = new Foo; } void main() {}private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); } Bye, bearophile
Sep 09 2011
On 9/9/11, bearophile <bearophileHUGS lycos.com> wrote:private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); }Oh right, that's the syntax. Thanks!
Sep 09 2011
On Friday, September 09, 2011 17:37:26 bearophile wrote:Andrej Mitrovic:But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.I need to have an object which is initialized only once, so I thought I could use immutable for that. But I can't do this: private class Foo {} immutable Foo foo; static this() { foo = new Foo; } void main() {}private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); }
Sep 09 2011
On 09/09/2011 11:42 PM, Jonathan M Davis wrote:On Friday, September 09, 2011 17:37:26 bearophile wrote:Shouldn't the compiler catch this?Andrej Mitrovic:But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.I need to have an object which is initialized only once, so I thought I could use immutable for that. But I can't do this: private class Foo {} immutable Foo foo; static this() { foo = new Foo; } void main() {}private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); }
Sep 09 2011
On Friday, September 09, 2011 23:44:10 Timon Gehr wrote:On 09/09/2011 11:42 PM, Jonathan M Davis wrote:It should, but it doesn't. http://d.puremagic.com/issues/show_bug.cgi?id=4923 http://d.puremagic.com/issues/show_bug.cgi?id=5207 http://d.puremagic.com/issues/show_bug.cgi?id=6114On Friday, September 09, 2011 17:37:26 bearophile wrote:Shouldn't the compiler catch this?Andrej Mitrovic:But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.I need to have an object which is initialized only once, so I thought I could use immutable for that. But I can't do this: private class Foo {} immutable Foo foo; static this() { foo = new Foo; } void main() {}private class Foo {} immutable Foo foo1; static this() { foo1 = new immutable(Foo); } void main() { auto foo2 = new immutable(Foo); }
Sep 09 2011
So much for that idea, immutable breaks property functions. Take a look: class Foo { this() { value = true; } property bool value() { return true; } property void value(bool value) { } } void main() { auto foo1 = new Foo; auto val1 = foo1.value; auto foo2 = new immutable(Foo); auto val2 = foo2.value; // fail } Is this a known issue or should I file it?
Sep 09 2011
On 9/9/11 11:45 PM, Andrej Mitrovic wrote:So much for that idea, immutable breaks property functions. Take a look: class Foo { this() { value = true; } property bool value() { return true; } property void value(bool value) { } } void main() { auto foo1 = new Foo; auto val1 = foo1.value; auto foo2 = new immutable(Foo); auto val2 = foo2.value; // fail } Is this a known issue or should I file it?Did you mark the getter const? David
Sep 09 2011
On 09/09/2011 11:45 PM, Andrej Mitrovic wrote:class Foo { this() { value = true; } property bool value() { return true; } property void value(bool value) { } }class Foo { this() { value = true; } property bool value() const { return true; } // const property void value(bool value) { } }
Sep 09 2011
On 9/9/11, Jonathan M Davis <jmdavisProg gmx.com> wrote:But make the constructor shared. Otherwise, it gets initialized once per thread in spite of the fact that immutable is implicitly shared.Good call!
Sep 09 2011
For crying out loud, shared fails too: class Foo { shared this() { value = true; } property bool value() { return true; } property void value(bool value) { } } void main() { auto foo1 = new Foo; auto val1 = foo1.value; // fail }
Sep 09 2011
On Friday, September 09, 2011 23:47:32 Andrej Mitrovic wrote:For crying out loud, shared fails too: class Foo { shared this() { value = true; } property bool value() { return true; } property void value(bool value) { } } void main() { auto foo1 = new Foo; auto val1 = foo1.value; // fail }I meant to make the static constructor shared.
Sep 09 2011
Ok so this is much more involved than I thought. I need to re-read parts of TDPL again. Sorry for the excessive noise. :p
Sep 09 2011