mirror of
https://github.com/dlang/phobos.git
synced 2025-04-28 22:21:09 +03:00
Move MostDerived and DerivedToFront to std.traits
Those two helpers are about type introspection and use MetaList only coincidently. Such utilities are better fit to std.traits
This commit is contained in:
parent
5cd03d229a
commit
3ca20b5ca4
2 changed files with 65 additions and 51 deletions
|
@ -37,56 +37,10 @@ public import std.meta.predicates;
|
|||
// shouldn't go into release
|
||||
public import std.meta.internal;
|
||||
|
||||
/**
|
||||
* Returns the type from TList that is the most derived from type T.
|
||||
* If none are found, T is returned.
|
||||
*/
|
||||
template MostDerived(T, TList...)
|
||||
{
|
||||
static if (TList.length == 0)
|
||||
alias MostDerived = T;
|
||||
else static if (is(TList[0] : T))
|
||||
alias MostDerived = MostDerived!(TList[0], TList[1 .. $]);
|
||||
else
|
||||
alias MostDerived = MostDerived!(T, TList[1 .. $]);
|
||||
}
|
||||
static import std.traits;
|
||||
|
||||
///
|
||||
unittest
|
||||
{
|
||||
class A { }
|
||||
class B : A { }
|
||||
class C : B { }
|
||||
alias Types = MetaList!(A, C, B);
|
||||
deprecated("Use std.traits.MostDerived instead")
|
||||
alias MostDerived = std.traits.MostDerived;
|
||||
|
||||
MostDerived!(Object, Types) x; // x is declared as type C
|
||||
static assert(is(typeof(x) == C));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list TList with the types sorted so that the most
|
||||
* derived types come first.
|
||||
*/
|
||||
template DerivedToFront(TList...)
|
||||
{
|
||||
static if (TList.length == 0)
|
||||
alias DerivedToFront = TList;
|
||||
else
|
||||
alias DerivedToFront =
|
||||
MetaList!(MostDerived!(TList[0], TList[1 .. $]),
|
||||
DerivedToFront!(ReplaceAll!(MostDerived!(TList[0], TList[1 .. $]),
|
||||
TList[0],
|
||||
TList[1 .. $])));
|
||||
}
|
||||
|
||||
///
|
||||
unittest
|
||||
{
|
||||
class A { }
|
||||
class B : A { }
|
||||
class C : B { }
|
||||
alias Types = MetaList!(A, C, B);
|
||||
|
||||
alias TL = DerivedToFront!(Types);
|
||||
static assert(is(TL == MetaList!(C, B, A)));
|
||||
}
|
||||
deprecated("Use std.traits.DerivedToFront instead")
|
||||
alias DerivedToFront = std.traits.DerivedToFront;
|
||||
|
|
60
std/traits.d
60
std/traits.d
|
@ -6383,3 +6383,63 @@ unittest
|
|||
static assert(is(typeof(a) == real));
|
||||
static assert(is(typeof(b) == real));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type from type list that is the most derived from type T.
|
||||
* If none are found, T is returned.
|
||||
*/
|
||||
template MostDerived(T, TList...)
|
||||
{
|
||||
static if (TList.length == 0)
|
||||
alias MostDerived = T;
|
||||
else static if (is(TList[0] : T))
|
||||
alias MostDerived = MostDerived!(TList[0], TList[1 .. $]);
|
||||
else
|
||||
alias MostDerived = MostDerived!(T, TList[1 .. $]);
|
||||
}
|
||||
|
||||
///
|
||||
unittest
|
||||
{
|
||||
import std.meta.list;
|
||||
|
||||
class A { }
|
||||
class B : A { }
|
||||
class C : B { }
|
||||
alias Types = MetaList!(A, C, B);
|
||||
|
||||
MostDerived!(Object, Types) x; // x is declared as type C
|
||||
static assert(is(typeof(x) == C));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type list based on TList with the types sorted so that
|
||||
* the most derived types come first.
|
||||
*/
|
||||
template DerivedToFront(TList...)
|
||||
{
|
||||
import std.meta.list;
|
||||
|
||||
static if (TList.length == 0)
|
||||
alias DerivedToFront = TList;
|
||||
else
|
||||
alias DerivedToFront =
|
||||
MetaList!(MostDerived!(TList[0], TList[1 .. $]),
|
||||
DerivedToFront!(ReplaceAll!(MostDerived!(TList[0], TList[1 .. $]),
|
||||
TList[0],
|
||||
TList[1 .. $])));
|
||||
}
|
||||
|
||||
///
|
||||
unittest
|
||||
{
|
||||
import std.meta.list;
|
||||
|
||||
class A { }
|
||||
class B : A { }
|
||||
class C : B { }
|
||||
alias Types = MetaList!(A, C, B);
|
||||
|
||||
alias TL = DerivedToFront!(Types);
|
||||
static assert(is(TL == MetaList!(C, B, A)));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue