File: tbindtypedesc.nim

package info (click to toggle)
nim 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 462,356 kB
  • sloc: sh: 11,089; ansic: 4,699; makefile: 706; python: 309; sql: 297; asm: 141; xml: 13
file content (88 lines) | stat: -rw-r--r-- 1,966 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
discard """
  msg: '''int int
float float
int int
TFoo TFoo
int float
TFoo TFoo'''
"""

import typetraits

type
  TFoo = object
    x, y: int

  TBar = tuple
    x, y: int

template accept(e) =
  static: assert(compiles(e))

template reject(e) =
  static: assert(not compiles(e))

proc genericParamRepeated[T: typedesc](a: T, b: T) =
  static:
    echo a.name, " ", b.name

accept genericParamRepeated(int, int)
accept genericParamRepeated(float, float)

reject genericParamRepeated(string, int)
reject genericParamRepeated(int, float)

proc genericParamOnce[T: typedesc](a, b: T) =
  static:
    echo a.name, " ", b.name

accept genericParamOnce(int, int)
accept genericParamOnce(TFoo, TFoo)

reject genericParamOnce(string, int)
reject genericParamOnce(TFoo, float)

type
  type1 = typedesc
  type2 = typedesc

proc typePairs(A, B: type1; C, D: type2) = discard

accept typePairs(int, int, TFoo, TFOO)
accept typePairs(TBAR, TBar, TBAR, TBAR)
accept typePairs(int, int, string, string)

reject typePairs(TBAR, TBar, TBar, TFoo)
reject typePairs(string, int, TBAR, TBAR)

proc typePairs2[T: typedesc, U: typedesc](A, B: T; C, D: U) = discard

accept typePairs2(int, int, TFoo, TFOO)
accept typePairs2(TBAR, TBar, TBAR, TBAR)
accept typePairs2(int, int, string, string)

reject typePairs2(TBAR, TBar, TBar, TFoo)
reject typePairs2(string, int, TBAR, TBAR)

proc dontBind(a: typedesc, b: typedesc) =
  static:
    echo a.name, " ", b.name

accept dontBind(int, float)
accept dontBind(TFoo, TFoo)

proc dontBind2(a, b: typedesc) = discard

accept dontBind2(int, float)
accept dontBind2(TBar, int)

proc bindArg(T: typedesc, U: typedesc, a, b: T, c, d: U) = discard

accept bindArg(int, string, 10, 20, "test", "nest")
accept bindArg(int, int, 10, 20, 30, 40)

reject bindArg(int, string, 10, "test", "test", "nest")
reject bindArg(int, int, 10, 20, 30, "test")
reject bindArg(int, string, 10.0, 20, "test", "nest")
reject bindArg(int, string, "test", "nest", 10, 20)