tfielditerator.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. discard """
  2. output: '''
  3. a char: true
  4. a char: false
  5. an int: 5
  6. an int: 6
  7. a string: abc
  8. false
  9. true
  10. true
  11. false
  12. true
  13. a: a
  14. b: b
  15. x: 5
  16. y: 6
  17. z: abc
  18. a char: true
  19. a char: false
  20. an int: 5
  21. an int: 6
  22. a string: abc
  23. a string: I'm root!
  24. CMP false
  25. CMP true
  26. CMP true
  27. CMP false
  28. CMP true
  29. CMP true
  30. a: a
  31. b: b
  32. x: 5
  33. y: 6
  34. z: abc
  35. thaRootMan: I'm root!
  36. myDisc: enC
  37. c: Z
  38. enC
  39. Z
  40. '''
  41. """
  42. block titerator1:
  43. type
  44. TMyTuple = tuple[a, b: char, x, y: int, z: string]
  45. proc p(x: char) = echo "a char: ", x <= 'a'
  46. proc p(x: int) = echo "an int: ", x
  47. proc p(x: string) = echo "a string: ", x
  48. var x: TMyTuple = ('a', 'b', 5, 6, "abc")
  49. var y: TMyTuple = ('A', 'b', 5, 9, "abc")
  50. for f in fields(x):
  51. p f
  52. for a, b in fields(x, y):
  53. echo a == b
  54. for key, val in fieldPairs(x):
  55. echo key, ": ", val
  56. doAssert x != y
  57. doAssert x == x
  58. doAssert(not (x < x))
  59. doAssert x <= x
  60. doAssert y < x
  61. doAssert y <= x
  62. block titerator2:
  63. type
  64. SomeRootObj = object of RootObj
  65. thaRootMan: string
  66. TMyObj = object of SomeRootObj
  67. a, b: char
  68. x, y: int
  69. z: string
  70. TEnum = enum enA, enB, enC
  71. TMyCaseObj = object
  72. case myDisc: TEnum
  73. of enA: a: int
  74. of enB: b: string
  75. of enC: c: char
  76. proc p(x: char) = echo "a char: ", x <= 'a'
  77. proc p(x: int) = echo "an int: ", x
  78. proc p(x: string) = echo "a string: ", x
  79. proc myobj(a, b: char, x, y: int, z: string): TMyObj =
  80. result.a = a; result.b = b; result.x = x; result.y = y; result.z = z
  81. result.thaRootMan = "I'm root!"
  82. var x = myobj('a', 'b', 5, 6, "abc")
  83. var y = myobj('A', 'b', 5, 9, "abc")
  84. for f in fields(x):
  85. p f
  86. for a, b in fields(x, y):
  87. echo "CMP ", a == b
  88. for key, val in fieldPairs(x):
  89. echo key, ": ", val
  90. var co = TMyCaseObj(myDisc: enC, c: 'Z')
  91. for key, val in fieldPairs(co):
  92. echo key, ": ", val
  93. for val in fields(co):
  94. echo val
  95. block:
  96. type
  97. Enum = enum A, B
  98. Object = object
  99. case a: Enum
  100. of A:
  101. integer: int
  102. of B:
  103. time: string
  104. let x = A
  105. let s = Object(a: x)
  106. doAssert s.integer == 0