__traits(comment) redux what if opt in?

Adam D. Ruppe destructionator at gmail.com
Sat May 30 23:04:42 UTC 2020


I still very much want access to ddoc from reflection. In the 
past, these proposals have been shot down out of fears of 
comments affecting code.

Well, what if it was ONLY available on the declaration itself?

__traits(comment) takes no arguments and returns a string with 
the contents of the attached documentation comment of the current 
declaration. It may return null if not compiling with -D (though 
I think it should be available regardless, I'm open to 
compromise).

If the function wants to make this available to outside code, it 
can use it in a UDA:

struct doc {
   string text;
}

/++ comment here +/
@doc(__traits(comment))
void foo() {}

assert(__traits(getAttributes, foo) == AliasSeq!(doc("/++ comment 
here +/")));


Would this be more amenable to people against this in the past? 
Since it is tightly scoped and opt in, there'd be very little 
room for abuse - it just basically copy/pastes the string to a 
UDA for you to reuse existing language mechanisms.


More information about the Digitalmars-d mailing list