"Timon Gehr" <timon.g...@gmx.ch> wrote in message news:is3rb5$1g32$1...@digitalmars.com... > > Nick Sabalausky wrote: >> >> "Timon Gehr" <timon.g...@gmx.ch> wrote in message >> news:is2lts$2fcn$1...@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(){} >
Hmm, something screwey seems to be going on. Your test example works for me too, but I could swear I've checked and double-checked everything (including the "__traits(compiles,...)") , and made your test case and my example as close to each other as I can, and I still can't seem to narrow down what's different. I do know this: In my code, I can't use typeof(this) because the compiler complains that "this" is only valid inside a member function. I have no idea why doing that seems to work, even for me, in your test case.