tconstobj.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. discard """
  2. output: '''
  3. (name: "hello")
  4. (-1, 0)
  5. (FirstName: "James", LastName: "Franco")
  6. [1, 2, 3]
  7. '''
  8. """
  9. # bug #2774, bug #3195
  10. type Foo = object
  11. name: string
  12. const fooArray = [
  13. Foo(name: "hello")
  14. ]
  15. echo fooArray[0]
  16. type
  17. Position = object
  18. x, y: int
  19. proc `$`(pos: Position): string =
  20. result = "(" & $pos.x & ", " & $pos.y & ")"
  21. proc newPos(x, y: int): Position =
  22. result = Position(x: x, y: y)
  23. const
  24. offset: array[1..4, Position] = [
  25. newPos(-1, 0),
  26. newPos(1, 0),
  27. newPos(0, -1),
  28. newPos(0, 1)
  29. ]
  30. echo offset[1]
  31. # bug #1547
  32. import tables
  33. type Person* = object
  34. FirstName*: string
  35. LastName*: string
  36. let people = {
  37. "001": Person(FirstName: "James", LastName: "Franco")
  38. }.toTable()
  39. echo people["001"]
  40. # Object downconversion should not copy
  41. type
  42. SomeBaseObj {.inheritable.} = object of RootObj
  43. txt : string
  44. InheritedFromBase = object of SomeBaseObj
  45. other : string
  46. proc initBase(sbo: var SomeBaseObj) =
  47. sbo.txt = "Initialized string from base"
  48. static:
  49. var ifb2: InheritedFromBase
  50. initBase(SomeBaseObj(ifb2))
  51. echo repr(ifb2)
  52. doAssert(ifb2.txt == "Initialized string from base")
  53. static: # issue #11861
  54. var ifb2: InheritedFromBase
  55. initBase(ifb2)
  56. doAssert(ifb2.txt == "Initialized string from base")
  57. static: # issue #15662
  58. proc a(T: typedesc) = echo T.type
  59. a((int, int))
  60. # bug #16069
  61. type
  62. E = enum
  63. val1, val2
  64. Obj = object
  65. case k: E
  66. of val1:
  67. x: array[3, int]
  68. of val2:
  69. y: uint32
  70. const
  71. foo = [1, 2, 3]
  72. arr = Obj(k: val1, x: foo)
  73. echo arr.x