[Issue 15371] New: __traits(getMember) should bypass the protection

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sat Nov 21 02:16:52 PST 2015


https://issues.dlang.org/show_bug.cgi?id=15371

          Issue ID: 15371
           Summary: __traits(getMember) should bypass the protection
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: bb.temp at gmx.com

"__traits(getMember,...)" should always work, whatever is the member protection
(as long as the symbol is a valid one).

The current limitation is problematic because it prevents to write functions
related to traits in a separate dedicated modules (e.g mylib.traits)

Every time a "__traits(getMember,...)" has to be used the only solution to make
it works with a private member is to write again and again the code (or to warp
the function in a mixin template).

Since there is also the traits verb "getProtection", it still would be possible
to respect the protection or not.

In case this wouldn't be clear, more concretly:

======================================
module mylib.traits;

mixin template ScopedReachability()
{
    bool isMemberReachableGood(T, string member)()
    if (is(T==class) || is(T==struct))
    {
        return __traits(compiles, __traits(getMember, T, member));
    }
} 

bool isMemberReachableBad(T, string member)()
if (is(T==class) || is(T==struct))
{
     return __traits(compiles, __traits(getMember, T, member));
}

=====================================
module somemodule;

class Foo
{
private: 
    uint a,b,c;
public:
    this()
    {
        foreach(member; __traits(allMembers, Foo))
        static if (isMemberReachableBad!(Foo, member)) {/*a,b,c not detected*/}

        import mylib.traits;
        mixin ScopedReachability; // isMemberReachableGood is now a local func. 
        static if (isMemberReachableGood!(Foo, member)) {/*a,b,c detected*/} 
    }
}

=====================================

With a relaxed protection policy on the "getMember" verb, it would be possible
to write reusable functions related to traits. Actually the problem prevents a
lot of good template that would simplify the traits usage to be written.

=====================================

see also:

- https://issues.dlang.org/show_bug.cgi?id=15335
- http://forum.dlang.org/post/xrrptwihnvlxabswnper@forum.dlang.org

--


More information about the Digitalmars-d-bugs mailing list