Skip to content

Commit

Permalink
Merge pull request #2116 from AndrejMitrovic/Fix11706
Browse files Browse the repository at this point in the history
Issue 11706 - Add a TypedefType trait to extract the underlying type of a std.typecons.Typedef
  • Loading branch information
Михаил Страшун committed Sep 8, 2014
2 parents 320d5e7 + f9048c9 commit bf8d579
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions std/typecons.d
Expand Up @@ -4467,6 +4467,10 @@ alias TypeFloat2 = Typedef!(float, float.init, "b");
// The two Typedefs are _not_ the same type.
static assert(!is(TypeFloat1 == TypeFloat2));
----
Note: If a library routine cannot handle the Typedef type,
you can use the $(D TypedefType) template to extract the
type which the Typedef wraps.
*/
struct Typedef(T, T init = T.init, string cookie=null)
{
Expand All @@ -4485,6 +4489,44 @@ struct Typedef(T, T init = T.init, string cookie=null)
mixin Proxy!Typedef_payload;
}

/**
Get the underlying type which a $(D Typedef) wraps.
If $(D T) is not a $(D Typedef) it will alias itself to $(D T).
*/
template TypedefType(T)
{
static if (is(T : Typedef!Arg, Arg))
alias TypedefType = Arg;
else
alias TypedefType = T;
}

///
unittest
{
import std.typecons: Typedef, TypedefType;
import std.conv: to;

alias MyInt = Typedef!int;
static assert(is(TypedefType!MyInt == int));

/// Instantiating with a non-Typedef will return that type
static assert(is(TypedefType!int == int));

string num = "5";

// extract the needed type
MyInt myInt = MyInt( num.to!(TypedefType!MyInt) );
assert(myInt == 5);

// cast to the underlying type to get the value that's being wrapped
int x = cast(TypedefType!MyInt)myInt;

alias MyIntInit = Typedef!(int, 42);
static assert(is(TypedefType!MyIntInit == int));
static assert(MyIntInit() == 42);
}

unittest
{
Typedef!int x = 10;
Expand Down

0 comments on commit bf8d579

Please sign in to comment.