mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 01:14:41 +00:00
[cleanup] remove disabled (and obsolete) ttypetraits; rename ttypetraits2 => ttypetraits (#13041)
* remove disabled (and obsolete) ttypetraits; rename ttypetraits2 => ttypetraits * D20200105T085828 fix super strange bug that causes CI to fail: builds.sr.ht with: `Error: Settle timed out after 120 attempts`
This commit is contained in:
committed by
Andreas Rumpf
parent
2cfa8d8385
commit
72499a8d7c
@@ -1,60 +1,44 @@
|
||||
discard """
|
||||
nimout: "int\nstring\nTBar[int]"
|
||||
output: "int\nstring\nTBar[int]\nint\nrange 0..2(int)\nstring"
|
||||
disabled: true
|
||||
"""
|
||||
|
||||
import typetraits
|
||||
|
||||
# simple case of type trait usage inside/outside of static blocks
|
||||
proc foo(x) =
|
||||
static:
|
||||
var t = type(x)
|
||||
echo t.name
|
||||
block: # isNamedTuple
|
||||
type Foo1 = (a:1,).type
|
||||
type Foo2 = (Field0:1,).type
|
||||
type Foo3 = ().type
|
||||
type Foo4 = object
|
||||
|
||||
echo x.type.name
|
||||
doAssert (a:1,).type.isNamedTuple
|
||||
doAssert Foo1.isNamedTuple
|
||||
doAssert Foo2.isNamedTuple
|
||||
doAssert not Foo3.isNamedTuple
|
||||
doAssert not Foo4.isNamedTuple
|
||||
doAssert not (1,).type.isNamedTuple
|
||||
|
||||
type
|
||||
TBar[U] = object
|
||||
x: U
|
||||
proc typeToString*(t: typedesc, prefer = "preferTypeName"): string {.magic: "TypeTrait".}
|
||||
## Returns the name of the given type, with more flexibility than `name`,
|
||||
## and avoiding the potential clash with a variable named `name`.
|
||||
## prefer = "preferResolved" will resolve type aliases recursively.
|
||||
# Move to typetraits.nim once api stabilized.
|
||||
|
||||
var bar: TBar[int]
|
||||
block: # typeToString
|
||||
type MyInt = int
|
||||
type
|
||||
C[T0, T1] = object
|
||||
type C2=C # alias => will resolve as C
|
||||
type C2b=C # alias => will resolve as C (recursively)
|
||||
type C3[U,V] = C[V,U]
|
||||
type C4[X] = C[X,X]
|
||||
template name2(T): string = typeToString(T, "preferResolved")
|
||||
doAssert MyInt.name2 == "int"
|
||||
doAssert C3[MyInt, C2b].name2 == "C3[int, C]"
|
||||
# C3 doesn't get resolved to C, not an alias (nor does C4)
|
||||
doAssert C2b[MyInt, C4[cstring]].name2 == "C[int, C4[cstring]]"
|
||||
doAssert C4[MyInt].name2 == "C4[int]"
|
||||
when BiggestFloat is float and cint is int:
|
||||
doAssert C2b[cint, BiggestFloat].name2 == "C3[int, C3[float, int32]]"
|
||||
|
||||
foo 10
|
||||
foo "test"
|
||||
foo bar
|
||||
|
||||
# generic params on user types work too
|
||||
proc foo2[T](x: TBar[T]) =
|
||||
echo T.name
|
||||
|
||||
foo2 bar
|
||||
|
||||
# less usual generic params on built-in types
|
||||
var arr: array[0..2, int] = [1, 2, 3]
|
||||
|
||||
proc foo3[R, T](x: array[R, T]) =
|
||||
echo name(R)
|
||||
|
||||
foo3 arr
|
||||
|
||||
const TypeList = [int, string, seq[int]]
|
||||
|
||||
macro selectType(inType: typedesc): typedesc =
|
||||
var typeSeq = @[float, TBar[int]]
|
||||
|
||||
for t in TypeList:
|
||||
typeSeq.add(t)
|
||||
|
||||
typeSeq.add(inType)
|
||||
typeSeq.add(type(10))
|
||||
|
||||
var typeSeq2: seq[typedesc] = @[]
|
||||
typeSeq2 = typeSeq
|
||||
|
||||
result = typeSeq2[5]
|
||||
|
||||
var xvar: selectType(string)
|
||||
xvar = "proba"
|
||||
echo xvar.type.name
|
||||
template name3(T): string = typeToString(T, "preferMixed")
|
||||
doAssert MyInt.name3 == "MyInt{int}"
|
||||
doAssert (tuple[a: MyInt, b: float]).name3 == "tuple[a: MyInt{int}, b: float]"
|
||||
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]"
|
||||
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
# todo: merge with $nimc_D/tests/metatype/ttypetraits.nim (currently disabled)
|
||||
|
||||
import typetraits
|
||||
|
||||
block: # isNamedTuple
|
||||
type Foo1 = (a:1,).type
|
||||
type Foo2 = (Field0:1,).type
|
||||
type Foo3 = ().type
|
||||
type Foo4 = object
|
||||
|
||||
doAssert (a:1,).type.isNamedTuple
|
||||
doAssert Foo1.isNamedTuple
|
||||
doAssert Foo2.isNamedTuple
|
||||
doAssert not Foo3.isNamedTuple
|
||||
doAssert not Foo4.isNamedTuple
|
||||
doAssert not (1,).type.isNamedTuple
|
||||
|
||||
proc typeToString*(t: typedesc, prefer = "preferTypeName"): string {.magic: "TypeTrait".}
|
||||
## Returns the name of the given type, with more flexibility than `name`,
|
||||
## and avoiding the potential clash with a variable named `name`.
|
||||
## prefer = "preferResolved" will resolve type aliases recursively.
|
||||
# Move to typetraits.nim once api stabilized.
|
||||
|
||||
block: # typeToString
|
||||
type MyInt = int
|
||||
type
|
||||
C[T0, T1] = object
|
||||
type C2=C # alias => will resolve as C
|
||||
type C2b=C # alias => will resolve as C (recursively)
|
||||
type C3[U,V] = C[V,U]
|
||||
type C4[X] = C[X,X]
|
||||
template name2(T): string = typeToString(T, "preferResolved")
|
||||
doAssert MyInt.name2 == "int"
|
||||
doAssert C3[MyInt, C2b].name2 == "C3[int, C]"
|
||||
# C3 doesn't get resolved to C, not an alias (nor does C4)
|
||||
doAssert C2b[MyInt, C4[cstring]].name2 == "C[int, C4[cstring]]"
|
||||
doAssert C4[MyInt].name2 == "C4[int]"
|
||||
when BiggestFloat is float and cint is int:
|
||||
doAssert C2b[cint, BiggestFloat].name2 == "C3[int, C3[float, int32]]"
|
||||
|
||||
template name3(T): string = typeToString(T, "preferMixed")
|
||||
doAssert MyInt.name3 == "MyInt{int}"
|
||||
doAssert (tuple[a: MyInt, b: float]).name3 == "tuple[a: MyInt{int}, b: float]"
|
||||
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]"
|
||||
Reference in New Issue
Block a user