Mixin templates accessing mixed out scope
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Sep 12 11:15:16 PDT 2015
On Saturday 12 September 2015 19:36, Prudence wrote:
> On Saturday, 12 September 2015 at 17:11:04 UTC, anonymous wrote:
[...]
>> class MyStore
>> {
>> class SingleStore
>> {
>> static void New() // Removing 'static' compiles
>> {
>> new SingleStore();
>> }
>> }
>> }
[...]
>> As for a fix: I guess SingleStore isn't supposed to be a nested
>> class. Mark it static then.
>
> NO! That is the whole point!
So New is supposed to create a SingleStore that's associated with a MyStore?
That static can't work like that then. It doesn't escape just the first
level (SingleStore), but all levels (SingleStore and MyStore). That is, a
static method isn't bound to any object. But you need a MyStore to construct
a (nested) SingleStore. You have to pass a MyStore somehow.
It can come via `this.outer`:
----
class MyStore
{
class SingleStore
{
SingleStore New()
{
return new SingleStore;
}
}
}
void main()
{
auto ms = new MyStore;
auto ss1 = ms.new SingleStore;
auto ss2 = ss1.New();
}
----
But here you need a SingleStore object to call New on. Not what you want, I
think.
Or the MyStore can come via parameter:
----
class MyStore
{
class SingleStore
{
static SingleStore New(MyStore ms)
{
return ms.new SingleStore;
}
}
}
void main()
{
auto ms = new MyStore;
auto ss = MyStore.SingleStore.New(ms);
}
----
Or you can move New a level up, into MyStore, and then plain `this` is the
needed MyStore:
----
class MyStore
{
class SingleStore
{
}
SingleStore NewSingleStore()
{
return new SingleStore;
}
}
void main()
{
auto ms = new MyStore;
auto ss = ms.NewSingleStore();
}
----
More information about the Digitalmars-d-learn
mailing list