@@ -14,3 +14,26 @@ block: # isNamedTuple
1414 doAssert not Foo3 .isNamedTuple
1515 doAssert not Foo4 .isNamedTuple
1616 doAssert not (1 ,).type .isNamedTuple
17+
18+ proc typeToString * (t: typedesc , prefer = " preferTypeName" ): string {.magic : " TypeTrait" .}
19+ # # Returns the name of the given type, with more flexibility than `name`,
20+ # # and avoiding the potential clash with a variable named `name`.
21+ # # prefer = "preferResolved" will resolve type aliases recursively.
22+ # Move to typetraits.nim once api stabilized.
23+
24+ block : # typeToString
25+ type MyInt = int
26+ type
27+ C[T0 , T1 ] = object
28+ type C2 = C # alias => will resolve as C
29+ type C2b = C # alias => will resolve as C (recursively)
30+ type C3 [U,V] = C[V,U]
31+ type C4 [X] = C[X,X]
32+ template name2 (T): string = typeToString (T, " preferResolved" )
33+ doAssert MyInt .name2 == " int"
34+ doAssert C3 [MyInt , C2b ].name2 == " C3[int, C]"
35+ # C3 doesn't get resolved to C, not an alias (nor does C4)
36+ doAssert C2b [MyInt , C4 [cstring ]].name2 == " C[int, C4[cstring]]"
37+ doAssert C4 [MyInt ].name2 == " C4[int]"
38+ when BiggestFloat is float and cint is int :
39+ doAssert C2b [cint , BiggestFloat ].name2 == " C3[int, C3[float, int32]]"
0 commit comments