Skip to content

Commit

Permalink
typetraits: fix #6454; genericParams; tuple len; tuple type get
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jan 8, 2020
1 parent 15043d3 commit d2c7029
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
38 changes: 38 additions & 0 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ proc distinctBase*(T: typedesc): typedesc {.magic: "TypeTrait".}
## Returns base type for distinct types, works only for distinct types.
## compile time error otherwise

import std/macros

macro len*(t: tuple): int =
## Return number of elements of `t`
newLit t.len

template len*(T: typedesc[tuple]): untyped =
## Return number of elements of `T`
len(default(T))

template get*(T: typedesc[tuple], i: static int): untyped =
## Return `i`th element of `T`
# Note: `[]` currently gives: `Error: no generic parameters allowed for ...`
type(default(T)[i])

macro genericParams*(T: typedesc): untyped =
## return tuple of generic params for generic `T`
runnableExamples:
type Foo[T1, T2]=object
doAssert genericParams(Foo[float, string]) is (float, string)
result = newNimNode(nnkTupleConstr)
var impl = getTypeImpl(T)
expectKind(impl, nnkBracketExpr)
impl = impl[1]
while true:
case impl.kind
of nnkSym:
impl = impl.getImpl
continue
of nnkTypeDef:
impl = impl[2]
continue
of nnkBracketExpr:
for i in 1..<impl.len:
result.add impl[i]
break
else:
error "wrong kind: " & $impl.kind

when isMainModule:
static:
Expand Down
22 changes: 16 additions & 6 deletions tests/metatype/ttypetraits.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typetraits
import macros

block: # isNamedTuple
block isNamedTuple:
type Foo1 = (a:1,).type
type Foo2 = (Field0:1,).type
type Foo3 = ().type
Expand All @@ -21,7 +21,7 @@ proc typeToString*(t: typedesc, prefer = "preferTypeName"): string {.magic: "Typ
## prefer = "preferResolved" will resolve type aliases recursively.
# Move to typetraits.nim once api stabilized.

block: # typeToString
block typeToString:
type MyInt = int
type
C[T0, T1] = object
Expand All @@ -44,9 +44,6 @@ block: # typeToString
doAssert (tuple[a: C2b[MyInt, C4[cstring]], b: cint, c: float]).name3 ==
"tuple[a: C2b{C}[MyInt{int}, C4[cstring]], b: cint{int32}, c: float]"


#----------------------------------------------------

block distinctBase:
block:
type
Expand Down Expand Up @@ -90,4 +87,17 @@ block distinctBase:
doAssert($distinctBase(typeof(b2)) == "string")
doAssert($distinctBase(typeof(c2)) == "int")


block genericParams:
type Foo[T1, T2]=object
doAssert genericParams(Foo[float, string]) is (float, string)
type Foo1 = Foo[float, int]
doAssert genericParams(Foo1) is (float, int)
type Foo2 = Foo[float, Foo1]
doAssert genericParams(Foo2) is (float, Foo[float, int])
doAssert genericParams(Foo2) is (float, Foo1)
doAssert genericParams(Foo2).get(1) is Foo1
doAssert (int,).get(0) is int
doAssert (int, float).get(1) is float
static: doAssert (int, float).len == 2
static: doAssert (1, ).len == 1
static: doAssert ().len == 0

0 comments on commit d2c7029

Please sign in to comment.