Skip to content

Commit

Permalink
Merge pull request #4650 from lodo1995/trait_is_inner_class
Browse files Browse the repository at this point in the history
New trait std.traits.isInnerClass
  • Loading branch information
andralex authored Aug 15, 2016
2 parents 098e52e + ebb74d5 commit c38d0d1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,54 @@ version (unittest)
// Aggregate Types
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::://

/**
Determines whether `T` is a class nested inside another class
and that `T.outer` is the implicit reference to the outer class
(i.e. `outer` has not been used as a field or method name)
Params:
T = type to test
Returns:
`true` if `T` is a class nested inside another, with the conditions described above;
`false` otherwise
*/
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 c38d0d1

Please sign in to comment.