Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Fix issue 4813 trait for getting at access modifiers #952

wants to merge 1 commit into from

6 participants

jmaschme Robert Clipsham Alex Rønne Petersen Andrei Alexandrescu Adam D. Ruppe Walter Bright

Added isPublic, isPrivate, isProtected, isPackage, and isExport traits

jmaschme jmaschme Fix issue 4813
Added isPublic, isPrivate, isProtected, isPackage, and isExport traits
Robert Clipsham

Would a getProtection trait not be more useful/flexible? Otherwise we'll end up with lots of wrappers for it unless they're added to phobos.


I think that would probably be best left for Phobos. There is no built-in representation for a protection type, so what would a getProtection trait return.

Also this approach is fairly consistent with the existing traits (isStaticFunction, isFinalFunction, isVirtualFunction, etc).

Robert Clipsham

I was thinking a string. It's fine either way I guess as long as the inverse is in phobos (with the current pull I'd expect a getProtection template in phobos which returns a string with the protection type).

Alex Rønne Petersen

Given that we have isVirtualFunction, isFinalFunction, and so on, I'd go for consistency and do it like this pull request does. It would have been better to have traits that returned strings for these things, but now we're stuck with the other approach, so let's be consistent.

Andrei Alexandrescu

I just reviewed a pull request that added protection level detection to __traits: #856. How do these two compare?

Adam D. Ruppe

I just tried using this DSYMBOL macro on my code, and it didn't pass an important test to me: it didn't give the protection on members returned from traits(getMember).

Adam D. Ruppe

I pushed an update to mine that passes the tests I need - in addition to the macro, gotta check for various dot expressions. Take a look at the pull request Andrei linked to.

Other than that, the difference is I return a string instead of doing is... meh either way works for me, the string just seemed simpler.

Hara Kenji 9rnsr referenced this pull request

Protection trait #856

Walter Bright

Closed as it is redundant with #856.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2012
  1. jmaschme

    Fix issue 4813

    jmaschme authored
    Added isPublic, isPrivate, isProtected, isPackage, and isExport traits
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 0 deletions.
  1. +5 −0 src/idgen.c
  2. +20 −0 src/traits.c
5 src/idgen.c
@@ -321,6 +321,11 @@ Msgtable msgtable[] =
{ "isRef" },
{ "isOut" },
{ "isLazy" },
+ { "isPublic" },
+ { "isPrivate" },
+ { "isProtected" },
+ { "isPackage" },
+ { "isExport" },
{ "hasMember" },
{ "identifier" },
{ "parent" },
20 src/traits.c
@@ -187,6 +187,26 @@ Expression *TraitsExp::semantic(Scope *sc)
ISDSYMBOL((d = s->isDeclaration()) != NULL && d->storage_class & STClazy)
+ else if (ident == Id::isPublic)
+ {
+ ISDSYMBOL((s->prot() == PROTpublic));
+ }
+ else if (ident == Id::isPrivate)
+ {
+ ISDSYMBOL((s->prot() == PROTprivate));
+ }
+ else if (ident == Id::isProtected)
+ {
+ ISDSYMBOL((s->prot() == PROTprotected));
+ }
+ else if (ident == Id::isPackage)
+ {
+ ISDSYMBOL((s->prot() == PROTpackage));
+ }
+ else if (ident == Id::isExport)
+ {
+ ISDSYMBOL((s->prot() == PROTexport));
+ }
else if (ident == Id::identifier)
{ // Get identifier for symbol as a string literal
Something went wrong with that request. Please try again.