Proposal: user defined attributes

Adam D. Ruppe destructionator at gmail.com
Fri Mar 16 09:34:17 PDT 2012


On Friday, 16 March 2012 at 16:09:55 UTC, Andrei Alexandrescu 
wrote:
> I wonder to what extent the in-language solution can be made to 
> work.

It can sort of work, but it isn't very good.

(I thought you might say this too; check out this post:
http://forum.dlang.org/thread/bccwycoexxykfgxvedix@forum.dlang.org#post-gmtawdmaihzgxnvezfrf:40forum.dlang.org 
)


In that other post, I talked about function parameters (which
is what I really want it for). The in-language solution for that
can only be made to work with a pile of fragile hacks.

But, even for functions, you just wrote:

     int a;
     mixin(note("a", Serializable.yes));

OK, that works... but what about:

     int a() { return 0; }
     mixin(note("a", Serializable.yes));


We're good so far... until:

     int a() { return 0; }
     int a(int b) { return 0; }
     mixin(note("a", Serializable.yes)); // which a?


You could potentially solve that by using the mangle of
instead of the string, and doing an alias template rather
than passing a string directly, but I'm not sure if we actually
can in today's dmd (I don't even know how to refer to
the proper overload...)

Also, what if the declaration is anonymous? You could
still see it in reflection - a ParameterTypeTuple doesn't
care about names - but you couldn't annotate it this
way. I guess a workaround there would be giving it a
name like _param_0.



Moreover, the mixin adds some junk to the struct. If
you iterate over allMembers, will you see

   enum _note_a_serializable;

in there? If there's a consistent naming convention,
we could skip it (my web.d ignores everything with a
leading underscore, for example), but it is nevertheless
letting implementation details slip out in reflection.

Seeing how the whole point of this is to be used in reflection,
that's a case I think is likely to cause annoyance in practice.



The current language solution isn't really *bad* with enough
library help, but it isn't particularly *good* either and I
don't think it can be. I've tried a few things, and I still
see the lack of user annotations as D's biggest miss right now.


More information about the Digitalmars-d mailing list