[Article Submission] Have Your Efficiency, and Flexibility Too

Timon Gehr timon.gehr at gmx.ch
Tue May 31 15:50:13 PDT 2011


Nick Sabalausky wrote:
>
> "Timon Gehr" <timon.gehr at gmx.ch> wrote in message
> news:is2lts$2fcn$1 at digitalmars.com...
> >
> > @Article: A very good read, it does not get boring even though it is quite
> > long. I
> > like it.
> >
>
> Thanks :)
>
> > Small nitpick:
> > mixin(declareInterface("IGizmo", "Gizmo!(numPorts, isSpinnable)"));
> >
> > It seems like this should be a mixin template, not a string mixin. Also
> > you
> > wouldn't normally want to specify ThisType. Use typeof(this):
> >
> > mixin template declareInterface(string interfaceName){
> >    mixin(`enum _this_implements_interface_`~interfaceName~`=true;`);
> >    mixin(`static assert(
> >               is`~interfaceName~`!(typeof(this)),
> >
> >               "This type fails to implement `~interfaceName~`"
> >           );`
> >    );
> > }
> >
> > and then you do just:
> > mixin declareInterface!"IGizmo";
> >
>
> I like the idea, but I just tried it and when I do that, 'isIGizmo' doesn't
> seem to be able to find the '_this_implements_interface_IGizmo' (using DMD
> 2.053). But, I wonder if that approach might be getting a little too fancy
> for this anyway. That would definitely be the right way to go for a real
> library, but for the article, the simpler it is to understand how it works,
> the better, and putting string mixins inside a template mixin is more to
> understand than just a string mixin (of course, I'm using CTFE too, but
> that's probably conceptually simpler for most people than template mixins).

Okay. Valid point. It is true that making the article more complicated is not
really an option. But you might want to add a comment that for serious work,
template mixins would be better suited (otherwise people will almost certainly
copy the string mixin approach ;))

It works for me. Are you sure you did not accidentally break some other part of
your __traits(compiles,...) ?

My minimal test case:

template isIGizmo(T){enum isIGizmo=__traits(compiles,{static
assert(T._this_implements_interface_IGizmo);});}

mixin template declareInterface(string interfaceName){
    mixin(`enum _this_implements_interface_`~interfaceName~`=true;`);
    mixin(`static assert(

               is`~interfaceName~`!(typeof(this)),

               "This type fails to implement `~interfaceName~`"

           );`
    );
}

struct Gizmo{mixin declareInterface!"IGizmo";}

static assert(isIGizmo!Gizmo);

void main(){}


Timon



More information about the Digitalmars-d mailing list