From f9048c94ef834b683b1ce30f5722c9e0969cdf74 Mon Sep 17 00:00:00 2001 From: Andrej Mitrovic Date: Thu, 24 Apr 2014 12:44:21 +0200 Subject: [PATCH] Fix Issue 11706 - Add a TypedefType trait to extract the underlying type of a std.typecons.Typedef. --- std/typecons.d | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/std/typecons.d b/std/typecons.d index a4d03e5b7a5..fe0da8c83c5 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -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) { @@ -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;