super(...) in mixin template

Steve Teale steve.teale at britseyeview.com
Mon Mar 3 21:08:44 PST 2014


On Monday, 3 March 2014 at 16:20:22 UTC, Gary Willoughby wrote:
> On Monday, 3 March 2014 at 16:11:48 UTC, Steve Teale wrote:
>> I did not get any takers in D Learn, so I'll try again here.
>>
>> Mixin templates are supposed to be instantiated in the scope 
>> where they were invoked. I have tried one that I want to 
>> invoke in the body of a constructor, but it fails when it 
>> calls super(...).
>>
>> Should this be so, or is it a bug?
>>
>> Steve
>
> Have you got an example please?

There's an example below, but it is probably not necessary.

The thing is that the mixin won't compile because it's seeing 
super(whatever) as an attempt to define a function.

mixin.d(35): Error: function declaration without return type. 
(Note that constructors are always named 'this')
mixin.d(35): Error: no identifier for declarator super(s, t, g)

If you move the mixin definition inside a constructor, you get a 
slew of other errors.

What I really want the mixin to do should I think be done after 
the class hierarchy is complete - without the super call the rest 
of it is hardly worth bothering.

I should also mention that the example below compiles OK if you 
move the super call out of the mixin, but for some reason it 
won't link - it works OK in my app, so I didn't pursue it.

import std.conv;

enum
{
    COX,
    WILLIAM,
    GREEN
}

enum Groups
{
    APPLES,
    PEARS,
    BANANAS
}

class App
{
    this();
}

class Base
{
    string name;
    int type;
    Groups group;

    mixin template Preamble(alias NAME, alias GNAME, alias T)
    {
       string s = NAME~" "~to!string(nextOid);
       Groups g = mixin("Groups."~GNAME);
       static int t = T;
       // Mixin must be syntactically correct - the following 
isn't because
       // we are not in a constructor
       super(s, t, g);
    }

    this(App app, string s, int t, Groups g)
    {
       name = s;
       type = t;
       group = g;
    }

}

class Intermediate : Base
{
    this(App app, string s, int t, Groups g)
    {
       super(app, s, t, g);
    }
}

class CoxPipin : Intermediate
{
    static int nextOid = 0;

    this(App app)
    {
       mixin Preamble!("CoxPipin", "APPLES", COX);
       // Move the super call out, and everything is fine
       //super(app, s, t, g);
    }
}

void main()
{
    App a = new App();
    Base x = new CoxPipin(a);
}


More information about the Digitalmars-d mailing list