test cases for the new typedesc features
This commit is contained in:
parent
badb6c0f66
commit
c899b51b52
2 changed files with 28 additions and 2 deletions
|
|
@ -1,10 +1,12 @@
|
||||||
import unittest
|
import unittest, typetraits
|
||||||
|
|
||||||
type
|
type
|
||||||
TFoo[T, U] = object
|
TFoo[T, U] = object
|
||||||
x: T
|
x: T
|
||||||
y: U
|
y: U
|
||||||
|
|
||||||
|
proc getTypeName(t: typedesc): string = t.name
|
||||||
|
|
||||||
proc foo(T: typedesc[float], a: expr): string =
|
proc foo(T: typedesc[float], a: expr): string =
|
||||||
result = "float " & $(a.len > 5)
|
result = "float " & $(a.len > 5)
|
||||||
|
|
||||||
|
|
@ -21,6 +23,9 @@ template foo(T: typedesc[seq]): expr = "seq"
|
||||||
test "types can be used as proc params":
|
test "types can be used as proc params":
|
||||||
# XXX: `check` needs to know that TFoo[int, float] is a type and
|
# XXX: `check` needs to know that TFoo[int, float] is a type and
|
||||||
# cannot be assigned for a local variable for later inspection
|
# cannot be assigned for a local variable for later inspection
|
||||||
|
check ((string.getTypeName == "string"))
|
||||||
|
check ((getTypeName(int) == "int"))
|
||||||
|
|
||||||
check ((foo(TFoo[int, float], 1000) == "TFoo 1000"))
|
check ((foo(TFoo[int, float], 1000) == "TFoo 1000"))
|
||||||
|
|
||||||
var f = 10.0
|
var f = 10.0
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
discard """
|
discard """
|
||||||
msg: "int\nstring\nTBar[int]"
|
msg: "int\nstring\nTBar[int]"
|
||||||
output: "int\nstring\nTBar[int]\nint\nrange 0..2"
|
output: "int\nstring\nTBar[int]\nint\nrange 0..2\nstring"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import typetraits
|
import typetraits
|
||||||
|
|
@ -36,3 +36,24 @@ proc foo3[R, T](x: array[R, T]) =
|
||||||
echo name(R)
|
echo name(R)
|
||||||
|
|
||||||
foo3 arr
|
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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue