summary refs log tree commit diff stats
path: root/tests/fields/tfields.nim
blob: 0bf3a4e1adf01bed2bfab6386dd72895273255ef (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
discard """
  output: '''
n
n
(one: 1, two: 2, three: 3)
1
2
3
(one: 4, two: 5, three: 6)
4
(one: 7, two: 8, three: 9)
7
8
9
(foo: 38, other: "string here")
43
100
90
'''
"""


block tindex:
  type
    TMyTuple = tuple[a, b: int]

  proc indexOf(t: typedesc, name: string): int =
    ## takes a tuple and looks for the field by name.
    ## returs index of that field.
    var
      d: t
      i = 0
    for n, x in fieldPairs(d):
      if n == name: return i
      i.inc
    raise newException(ValueError, "No field " & name & " in type " &
      astToStr(t))

  doAssert TMyTuple.indexOf("b") == 1



block ttemplate:
  # bug #1902
  # This works.
  for name, value in (n: "v").fieldPairs:
    echo name

  template wrapper(): void =
    for name, value in (n: "v").fieldPairs:
      echo name
  wrapper()



block tbreak:
  # bug #2134
  type
    TestType = object
      one: int
      two: int
      three: int

  var
    ab = TestType(one:1, two:2, three:3)
    ac = TestType(one:4, two:5, three:6)
    ad = TestType(one:7, two:8, three:9)
    tstSeq = [ab, ac, ad]

  for tstElement in mitems(tstSeq):
    echo tstElement
    for tstField in fields(tstElement):
      #for tstField in [1,2,4,6]:
      echo tstField
      if tstField == 4:
        break



block timplicit_with_partial:
  type
    Base = ref object of RootObj
    Foo {.partial.} = ref object of Base

  proc my(f: Foo) =
    #var f.next = f
    let f.foo = 38
    let f.other = "string here"
    echo f[]
    echo f.foo + 5

  var g: Foo
  new(g)
  my(g)

  type
    FooTask {.partial.} = ref object of RootObj

  proc foo(t: FooTask) {.liftLocals: t.} =
    var x = 90
    if true:
      var x = 10
      while x < 100:
        inc x
      echo x
    echo x

  foo(FooTask())