UDA example

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Mon Aug 19 11:21:24 PDT 2013


On Monday, 19 August 2013 at 18:01:40 UTC, Jacob Carlborg wrote:
> On 2013-08-19 19:57, Craig Dillabaugh wrote:
>> When I attempt to compile the following example code from the
>> section on UDAs
>> at:  http://dlang.org/attribute.html
>>
>> void main(string args[]) {
>> template Tuple (T...)
>> {
>>          alias T Tuple;
>> }
>>
>> enum EEE = 7;
>> @("hello") struct SSS { }
>> @(3) { @(4) @EEE @SSS int foo; }
>>
>> alias Tuple!(__traits(getAttributes, foo)) TP;
>>
>> pragma(msg, TP); // prints tuple(3, 4, 7, (SSS))
>> pragma(msg, TP[2]); // prints 7
>> }
>>
>> I get the following errors:
>>
>> UDAs.d(9): Error: basic type expected, not {
>> UDAs.d(9): Error: no identifier for declarator int
>> UDAs.d(9): Error: semicolon expected, not '{'
>>
>> Line 9 is the one starting "@(3) ..."
>>
>> Is this due to:
>>         a. a DMD bug (DMD64 D Compiler v2.063.2, Linux)
>>         b. a Documentation bug
>>         c. me not properly copying/pasting the example :/
>>
>
> Move everything out of "main" and it will work.

So I guess it was option (c) after all.

I am still curious though was is wrong using UDAs inside main().
If I have the following code inside main.

     @(7) int b;
     pragma(msg, __traits(getAttributes, b) );

or
     @("goodbye") struct D {}
     pragma(msg, __traits(getAttributes, D) );

Then it compiles fine.  But if I make it:

     @(7) { @{11} int b; }
     pragma(msg, __traits(getAttributes, b) );

Then I get the same error.

Craig




More information about the Digitalmars-d-learn mailing list