[Article Submission] Have Your Efficiency, and Flexibility Too

Nick Sabalausky a at a.a
Tue May 31 16:32:59 PDT 2011


"Nick Sabalausky" <a at a.a> wrote in message 
news:is3tk5$1j2n$1 at digitalmars.com...
> "Timon Gehr" <timon.gehr at gmx.ch> wrote in message 
> news:is3rb5$1g32$1 at digitalmars.com...
>>
>> 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.
>

I think typeof(this) just seems to be screwy. Check this out:

struct Gizmo {
    static assert( is(typeof(this)==Gizmo) );
}
void main() {}

Error: static assert  (is(typeof(__error) == Gizmo)) is false

But this is fine:

struct Gizmo {
    alias typeof(this) Foo;
    typeof(this)* ptr;
}




More information about the Digitalmars-d mailing list