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.

Reply via email to