tconstobj.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. discard """
  2. output: '''
  3. (name: "hello")
  4. (-1, 0)
  5. (FirstName: "James", LastName: "Franco")
  6. '''
  7. """
  8. # bug #2774, bug #3195
  9. type Foo = object
  10. name: string
  11. const fooArray = [
  12. Foo(name: "hello")
  13. ]
  14. echo fooArray[0]
  15. type
  16. Position = object
  17. x, y: int
  18. proc `$`(pos: Position): string =
  19. result = "(" & $pos.x & ", " & $pos.y & ")"
  20. proc newPos(x, y: int): Position =
  21. result = Position(x: x, y: y)
  22. const
  23. offset: array[1..4, Position] = [
  24. newPos(-1, 0),
  25. newPos(1, 0),
  26. newPos(0, -1),
  27. newPos(0, 1)
  28. ]
  29. echo offset[1]
  30. # bug #1547
  31. import tables
  32. type Person* = object
  33. FirstName*: string
  34. LastName*: string
  35. let people = {
  36. "001": Person(FirstName: "James", LastName: "Franco")
  37. }.toTable()
  38. echo people["001"]
  39. # Object downconversion should not copy
  40. type
  41. SomeBaseObj {.inheritable.} = object of RootObj
  42. txt : string
  43. InheritedFromBase = object of SomeBaseObj
  44. other : string
  45. proc initBase(sbo: var SomeBaseObj) =
  46. sbo.txt = "Initialized string from base"
  47. static:
  48. var ifb2: InheritedFromBase
  49. initBase(SomeBaseObj(ifb2))
  50. echo repr(ifb2)
  51. doAssert(ifb2.txt == "Initialized string from base")
  52. static: # issue #11861
  53. var ifb2: InheritedFromBase
  54. initBase(ifb2)
  55. doAssert(ifb2.txt == "Initialized string from base")