Why the @ in @safe? & UDAs

Jacob Carlborg doob at me.com
Wed Nov 6 23:36:26 PST 2013


On 2013-11-07 01:13, Shammah Chancellor wrote:
> My understanding is that @ attributes were for used-defined behavior
> only.   Pure and nothrow, since the compiler implements behavior on
> those keywords, do not have the @ sign.   So, why is @safe, @safe, and
> not just "safe".
>
> On the basis that @attributes should not have compiler defined
> behavior.   Please take off the @ from the @safe.   This would apply to
> @trusted as well.

The reason is basically that they wanted a new scope for keywords. 
Making it easy to introduce new keywords without breaking existing code. 
This worked until UDA's where introduce, because now "@foo" is legal. 
Now we're back at square one, although we do have UDA's, which are nice :)

> Also,  I think that having UDAs (user defined attributes) be freeform,
> and not a class following some interface is asking for trouble.  It
> makes them hard to use reflection with, and it also causes the potential
> for modules to use the same literal for different purposes. I think C#
> has this right.

I completely agree. The recommendation is to not use literals but 
instead create a new type for the UDA:

enum foo;

@foo void bar () { }

Or:

struct foo { string name; }

@foo("asd") void bar () { }

This way you can uniquely identify a UDA using its fully qualified name. 
On top of that I'm using some helper functions which forces me to 
explicitly mark any UDA with a special UDA.

enum attribute;

@attribute enum foo;

@attribute struct foo { string name; }

@foo("asd") void bar () { }

getAttributes!(bar);

By default "getAttributes" will only return attributes which themselves 
have the @attribute UDA attached to them. See 
https://github.com/jacob-carlborg/mambo/blob/master/mambo/core/Attribute.d

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list