UDA example

Dicebot public at dicebot.lv
Mon Aug 19 11:28:22 PDT 2013


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.


More information about the Digitalmars-d-learn mailing list