www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - shared class constructor

reply Jeremie Pelletier <jeremiep gmail.com> writes:
Just came across this while porting my code to enforce the use of shared.

---
module test;
shared class Foo {
   this() {}
}
---
Error: cannot implicitly convert expression (this) of type shared(Foo) to
test.Foo

Of course I could just use new shared(Foo) but that would allow non-shared
instances of Foo to be created as well, which I do not want.

Shouldn't "this" resolve to the fully qualified class?
Aug 13 2009
parent "Robert Jacques" <sandford jhu.edu> writes:
On Thu, 13 Aug 2009 09:34:50 -0700, Jeremie Pelletier <jeremiep gmail.com>  
wrote:

 Just came across this while porting my code to enforce the use of shared.

 ---
 module test;
 shared class Foo {
    this() {}
 }
 ---
 Error: cannot implicitly convert expression (this) of type shared(Foo)  
 to test.Foo

 Of course I could just use new shared(Foo) but that would allow  
 non-shared instances of Foo to be created as well, which I do not want.

 Shouldn't "this" resolve to the fully qualified class?

In general, shared classes right now are broken. For example, you can't actually write any shared methods yet, (bug 3089) amoung other things. This does look like something new, and should be added. As for your question: given that immutable classes work the same way, I assume it should.
Aug 13 2009