mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 15:10:46 +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
|
// shouldn't go into release
|
||||||
public import std.meta.internal;
|
public import std.meta.internal;
|
||||||
|
|
||||||
/**
|
static import std.traits;
|
||||||
* 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 .. $]);
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
deprecated("Use std.traits.MostDerived instead")
|
||||||
unittest
|
alias MostDerived = std.traits.MostDerived;
|
||||||
{
|
|
||||||
class A { }
|
|
||||||
class B : A { }
|
|
||||||
class C : B { }
|
|
||||||
alias Types = MetaList!(A, C, B);
|
|
||||||
|
|
||||||
MostDerived!(Object, Types) x; // x is declared as type C
|
deprecated("Use std.traits.DerivedToFront instead")
|
||||||
static assert(is(typeof(x) == C));
|
alias DerivedToFront = std.traits.DerivedToFront;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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)));
|
|
||||||
}
|
|
||||||
|
|
60
std/traits.d
60
std/traits.d
|
@ -6383,3 +6383,63 @@ unittest
|
||||||
static assert(is(typeof(a) == real));
|
static assert(is(typeof(a) == real));
|
||||||
static assert(is(typeof(b) == 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