Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 19, 2013 UDA example | ||||
---|---|---|---|---|
| ||||
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 :/ Craig P.S. This reCAPTCHA anti-spam think sucks. |
August 19, 2013 Re: UDA example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | 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. -- /Jacob Carlborg |
August 19, 2013 Re: UDA example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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
|
August 19, 2013 Re: UDA example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | On Monday, 19 August 2013 at 18:21:26 UTC, Craig Dillabaugh wrote:
> I am still curious though was is wrong using UDAs inside main().
It is not exactly about UDA - key is different meaning of curly brace {} syntax in function bodies and in declaration scopes.
Used in declaration scope {} apply all attributes attached to it to every declaration inside:
const
{
int a;
double b;
}
static assert (is(typeof(a) == const));
This also applies for UDA's.
In function body it creates new variable scope - variables declared inside get destroyed upon leaving the scope and are not available outside.
void foo()
{
{
int a;
}
// a = 42; // error
}
As this usage of {} does not expect any attribute propagation, UDA's get rejected too.
|
August 19, 2013 Re: UDA example | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Monday, 19 August 2013 at 18:28:24 UTC, Dicebot wrote:
> On Monday, 19 August 2013 at 18:21:26 UTC, Craig Dillabaugh wrote:
>> I am still curious though was is wrong using UDAs inside main().
>
> It is not exactly about UDA - key is different meaning of curly brace {} syntax in function bodies and in declaration scopes.
>
> Used in declaration scope {} apply all attributes attached to it to every declaration inside:
>
> const
> {
> int a;
> double b;
> }
>
> static assert (is(typeof(a) == const));
>
> This also applies for UDA's.
>
> In function body it creates new variable scope - variables declared inside get destroyed upon leaving the scope and are not available outside.
>
> void foo()
> {
> {
> int a;
> }
> // a = 42; // error
> }
>
> As this usage of {} does not expect any attribute propagation, UDA's get rejected too.
Thank you, makes perfect sense.
|
Copyright © 1999-2021 by the D Language Foundation