|
14 | 14 |
|
15 | 15 | export system.`$` # for backward compatibility
|
16 | 16 |
|
17 |
| -include "system/inclrtl" |
18 |
| - |
19 | 17 | proc name*(t: typedesc): string {.magic: "TypeTrait".}
|
20 | 18 | ## Returns the name of the given type.
|
21 | 19 | ##
|
@@ -71,46 +69,6 @@ proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".}
|
71 | 69 | ## Returns base type for distinct types, works only for distinct types.
|
72 | 70 | ## compile time error otherwise
|
73 | 71 |
|
74 |
| -import std/macros |
75 |
| - |
76 |
| -macro lenTuple*(t: tuple): int {.since: (1, 1).} = |
77 |
| - ## Return number of elements of `t` |
78 |
| - newLit t.len |
79 |
| - |
80 |
| -macro lenTuple*(t: typedesc[tuple]): int {.since: (1, 1).} = |
81 |
| - ## Return number of elements of `T` |
82 |
| - newLit t.len |
83 |
| - |
84 |
| -since (1, 1): |
85 |
| - template get*(T: typedesc[tuple], i: static int): untyped = |
86 |
| - ## Return `i`th element of `T` |
87 |
| - # Note: `[]` currently gives: `Error: no generic parameters allowed for ...` |
88 |
| - type(default(T)[i]) |
89 |
| - |
90 |
| -macro genericParams*(T: typedesc): untyped {.since: (1, 1).} = |
91 |
| - ## return tuple of generic params for generic `T` |
92 |
| - runnableExamples: |
93 |
| - type Foo[T1, T2]=object |
94 |
| - doAssert genericParams(Foo[float, string]) is (float, string) |
95 |
| - result = newNimNode(nnkTupleConstr) |
96 |
| - var impl = getTypeImpl(T) |
97 |
| - expectKind(impl, nnkBracketExpr) |
98 |
| - impl = impl[1] |
99 |
| - while true: |
100 |
| - case impl.kind |
101 |
| - of nnkSym: |
102 |
| - impl = impl.getImpl |
103 |
| - continue |
104 |
| - of nnkTypeDef: |
105 |
| - impl = impl[2] |
106 |
| - continue |
107 |
| - of nnkBracketExpr: |
108 |
| - for i in 1..<impl.len: |
109 |
| - result.add impl[i] |
110 |
| - break |
111 |
| - else: |
112 |
| - error "wrong kind: " & $impl.kind |
113 |
| - |
114 | 72 | when isMainModule:
|
115 | 73 | static:
|
116 | 74 | doAssert $type(42) == "int"
|
|
0 commit comments