Skip to content

Commit

Permalink
Implemented isInnerClass trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Lodovico Giaretta authored and Lodovico Giaretta committed Jul 24, 2016
1 parent 7fde2c6 commit 0ecc086
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,47 @@ version (unittest)
// Aggregate Types
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://

/**
Determines whether $(D T) is a class nested inside another class
and that $(D T.outer) is the implicit reference to the outer class
(i.e. $(D outer) has not been used as a field or method name)
*/
template isInnerClass(T)
if (is(T == class))
{
import std.meta: staticIndexOf;

enum isInnerClass = __traits(isSame, typeof(T.outer), __traits(parent, T))
&& (staticIndexOf!(__traits(allMembers, T), "outer") == -1);
}

///
@safe unittest
{
class C
{
int outer;
}
static assert(!isInnerClass!C);

class Outer1
{
class Inner
{
}
}
static assert(isInnerClass!(Outer1.Inner));

class Outer2
{
class Inner
{
int outer;
}
}
static assert(!isInnerClass!(Outer2.Inner));
}

/**
Determines whether $(D T) has its own context pointer.
$(D T) must be either $(D class), $(D struct), or $(D union).
Expand Down

0 comments on commit 0ecc086

Please sign in to comment.