Another use case for __traits(docComment)

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 11 20:15:56 UTC 2019


On 11/11/19 12:54 PM, Adam D. Ruppe wrote:
> On Monday, 11 November 2019 at 17:16:20 UTC, Steven Schveighoffer wrote:
>> I still am very VERY leery of having anything in comments affect the 
>> code in any way.
> 
> then just.... don't do that.
> 
> And btw for the record you could have comments affect code right now:
> 
> mixin((__LINE__ % 3) ? "one thing" : "other thing"); // adding a comment 
> above would change the line number and possibly trigger the other thing!

Haha, yeah, that is what I don't want.

But I think we can safely say that using comments to affect code can 
result in reasonable looking things (the above is NOT reasonable) that 
can be exploited by adjusting the comments.

Certainly, it could be allowed at compile-time and just be a guideline 
not to write code that cares about the data. But we all know that it 
will happen.

For instance, I like how documented unit tests simply turns the unittest 
into a ddoc, instead of making ddoc turn into a unit test (which likely 
could have been the solution if __traits(docComment) were available back 
then). It's the correct translation, because code can always be turned 
into comments, but the other way around is questionable.

> The same thing is true of documentation comments. You *could* abuse... 
> but why would you? UDAs are easier to use for other purposes anyway. 
> (just right now we are using UDAs for docs just because it is needed 
> elsewhere!)

One thing that's different from __LINE__ is that __LINE__ is only 
provided to that line of code. It's not accessible outside the line. 
__traits(docComment, x) would provide access to the comments from 
elsewhere. So it may be external abuse that happens.

> 
>> What about a compromise? Provide access to the docs but ONLY at 
>> runtime. That is, you will not know anything about the string at 
>> compiletime, just at runtime, and you can make your decisions there.
> 
> Yeah, that'd work for this case but it is a pointless restriction.

It would be something that could potentially be expanded later if 
needed. It's easier to let the toothpaste out of the tube slowly than 
try to put some of it back in.

-Steve


More information about the Digitalmars-d mailing list