[Article Submission] Have Your Efficiency, and Flexibility Too

Steven Schveighoffer schveiguy at yahoo.com
Wed Jun 1 07:07:11 PDT 2011


On Tue, 31 May 2011 19:32:59 -0400, Nick Sabalausky <a at a.a> wrote:

> "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;
> }

I think there are certain special situations where you can use  
typeof(this).  For example, as the return type for a static method.

*looks for doc*  Couldn't find any documentation on it...

It's somewhat like static this, which is inflexible in how you write it.

-Steve


More information about the Digitalmars-d mailing list