Merge branch 'master' of github.com:Araq/Nimrod
This commit is contained in:
commit
8710118b2c
22 changed files with 384 additions and 133 deletions
87
tests/compile/tbindtypedesc.nim
Normal file
87
tests/compile/tbindtypedesc.nim
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
discard """
|
||||
msg: '''
|
||||
int
|
||||
float
|
||||
TFoo
|
||||
TFoo
|
||||
'''
|
||||
"""
|
||||
|
||||
import typetraits
|
||||
|
||||
type
|
||||
TFoo = object
|
||||
x, y: int
|
||||
|
||||
TBar = tuple
|
||||
x, y: int
|
||||
|
||||
template good(e: expr) =
|
||||
static: assert(compiles(e))
|
||||
|
||||
template bad(e: expr) =
|
||||
static: assert(not compiles(e))
|
||||
|
||||
proc genericParamRepeated[T: typedesc](a: T, b: T) =
|
||||
static:
|
||||
echo a.name
|
||||
echo b.name
|
||||
|
||||
good(genericParamRepeated(int, int))
|
||||
good(genericParamRepeated(float, float))
|
||||
|
||||
bad(genericParamRepeated(string, int))
|
||||
bad(genericParamRepeated(int, float))
|
||||
|
||||
proc genericParamOnce[T: typedesc](a, b: T) =
|
||||
static:
|
||||
echo a.name
|
||||
echo b.name
|
||||
|
||||
good(genericParamOnce(int, int))
|
||||
good(genericParamOnce(TFoo, TFoo))
|
||||
|
||||
bad(genericParamOnce(string, int))
|
||||
bad(genericParamOnce(TFoo, float))
|
||||
|
||||
proc typePairs(A, B: type1; C, D: type2) = nil
|
||||
|
||||
good(typePairs(int, int, TFoo, TFOO))
|
||||
good(typePairs(TBAR, TBar, TBAR, TBAR))
|
||||
good(typePairs(int, int, string, string))
|
||||
|
||||
bad(typePairs(TBAR, TBar, TBar, TFoo))
|
||||
bad(typePairs(string, int, TBAR, TBAR))
|
||||
|
||||
proc typePairs2[T: typedesc, U: typedesc](A, B: T; C, D: U) = nil
|
||||
|
||||
good(typePairs2(int, int, TFoo, TFOO))
|
||||
good(typePairs2(TBAR, TBar, TBAR, TBAR))
|
||||
good(typePairs2(int, int, string, string))
|
||||
|
||||
bad(typePairs2(TBAR, TBar, TBar, TFoo))
|
||||
bad(typePairs2(string, int, TBAR, TBAR))
|
||||
|
||||
proc dontBind(a: typedesc, b: typedesc) =
|
||||
static:
|
||||
echo a.name
|
||||
echo b.name
|
||||
|
||||
good(dontBind(int, float))
|
||||
good(dontBind(TFoo, TFoo))
|
||||
|
||||
proc dontBind2(a, b: typedesc) = nil
|
||||
|
||||
good(dontBind2(int, float))
|
||||
good(dontBind2(TBar, int))
|
||||
|
||||
proc bindArg(T: typedesc, U: typedesc, a, b: T, c, d: U) = nil
|
||||
|
||||
good(bindArg(int, string, 10, 20, "test", "nest"))
|
||||
good(bindArg(int, int, 10, 20, 30, 40))
|
||||
|
||||
bad(bindArg(int, string, 10, "test", "test", "nest"))
|
||||
bad(bindArg(int, int, 10, 20, 30, "test"))
|
||||
bad(bindArg(int, string, 10.0, 20, "test", "nest"))
|
||||
bad(bindArg(int, string, "test", "nest", 10, 20))
|
||||
|
||||
30
tests/compile/tgenericshardcases.nim
Normal file
30
tests/compile/tgenericshardcases.nim
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
discard """
|
||||
file: "tgenericshardcases.nim"
|
||||
output: "int\nfloat\nint\nstring"
|
||||
"""
|
||||
|
||||
import typetraits
|
||||
|
||||
proc typeNameLen(x: typedesc): int {.compileTime.} =
|
||||
result = x.name.len
|
||||
|
||||
macro selectType(a, b: typedesc): typedesc =
|
||||
result = a
|
||||
|
||||
type
|
||||
Foo[T] = object
|
||||
data1: array[high(T), int]
|
||||
data2: array[1..typeNameLen(T), selectType(float, string)]
|
||||
|
||||
MyEnum = enum A, B, C,D
|
||||
|
||||
var f1: Foo[MyEnum]
|
||||
var f2: Foo[int8]
|
||||
|
||||
static:
|
||||
assert high(f1.data1) == D
|
||||
assert high(f1.data2) == 6 # length of MyEnum
|
||||
|
||||
assert high(f2.data1) == 127
|
||||
assert high(f2.data2) == 4 # length of int8
|
||||
|
||||
39
tests/run/tmacrogenerics.nim
Normal file
39
tests/run/tmacrogenerics.nim
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
discard """
|
||||
file: "tmacrogenerics.nim"
|
||||
msg: '''
|
||||
instantiation 1 with int and float
|
||||
instantiation 2 with float and string
|
||||
instantiation 3 with string and string
|
||||
counter: 3
|
||||
'''
|
||||
output: "int\nfloat\nint\nstring"
|
||||
"""
|
||||
|
||||
import typetraits, macros
|
||||
|
||||
var counter {.compileTime.} = 0
|
||||
|
||||
macro makeBar(A, B: typedesc): typedesc =
|
||||
inc counter
|
||||
echo "instantiation ", counter, " with ", A.name, " and ", B.name
|
||||
result = A
|
||||
|
||||
type
|
||||
Bar[T, U] = makeBar(T, U)
|
||||
|
||||
var bb1: Bar[int, float]
|
||||
var bb2: Bar[float, string]
|
||||
var bb3: Bar[int, float]
|
||||
var bb4: Bar[string, string]
|
||||
|
||||
proc match(a: int) = echo "int"
|
||||
proc match(a: string) = echo "string"
|
||||
proc match(a: float) = echo "float"
|
||||
|
||||
match(bb1)
|
||||
match(bb2)
|
||||
match(bb3)
|
||||
match(bb4)
|
||||
|
||||
static:
|
||||
echo "counter: ", counter
|
||||
|
|
@ -1,17 +1,22 @@
|
|||
discard """
|
||||
file: "tstaticparams.nim"
|
||||
output: "abracadabra\ntest"
|
||||
output: "abracadabra\ntest\n3"
|
||||
"""
|
||||
|
||||
type
|
||||
TFoo[T; Val: expr[string]] = object
|
||||
data: array[4, T]
|
||||
|
||||
TBar[T; I: expr[int]] = object
|
||||
data: array[I, T]
|
||||
|
||||
proc takeFoo(x: TFoo) =
|
||||
echo "abracadabra"
|
||||
echo TFoo.Val
|
||||
|
||||
var x: TFoo[int, "test"]
|
||||
|
||||
takeFoo(x)
|
||||
|
||||
var y: TBar[float, 4]
|
||||
echo high(y.data)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue