On 1 April 2012 21:27, Adam D. Ruppe <destructiona...@gmail.com> wrote:
> I've prepared a dmd pull request to add a new __trait: > getProtection. > > It is meant to be used along with getMember() to add to > the reflection capabilities, letting us use the existing > protection qualifiers as strings. > > From the test: > == > class Test { > public int a; > private int b; > export string c; > protected int d; > package void e() {} > } > > void main() { > Test t; > static assert(__traits(getProtection, __traits(getMember, t, "a")) == > "public"); > static assert(__traits(getProtection, __traits(getMember, t, "b")) == > "private"); > static assert(__traits(getProtection, __traits(getMember, t, "c")) == > "export"); > static assert(__traits(getProtection, __traits(getMember, t, "d")) == > "protected"); > static assert(__traits(getProtection, __traits(getMember, t, "e")) == > "package"); > } > == > > > > This will help D automatically generate things like > external interfaces that use the protections. > > For instance, I plan to use it in my web.d to only > make functions marked "export" available via the > web interface. Currently, you have to use a naming > convention to hide functions - a leading underscore - > even on private members. This is ok, but not great. > > But with the protection trait, we can mark it with > a much more natural "private", or any of the other > specifiers D has. > > > I'm sure other uses will come up too. > Wow, it so happens I need to use this immediately (tomorrow), and I assumed it was already possible... I wonder about the protection flags used this way though. I want to generate external interfaces for all public stuff, but it occurred to me that an explicit modifier like export would be more appropriate to control what is exposed. The problem with 'export' is it's already defined to export from a shared object/dll, and my code IS a dll, so export is not something I can overload for this purpose... export(dll) maybe? Then you could describe other export types... ? ... this is another perfect use for... custom attributes! ;) I also need to tag functions/classes/methods/properties as being saved, state recorded, editor exposed, script bound... I have a bucket load of needs for custom attributes.