tcursor_field_obj_constr.nim 814 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. discard """
  2. output: '''a
  3. b
  4. c'''
  5. cmd: "nim c --gc:arc $file"
  6. """
  7. # bug #18469
  8. type
  9. Edge = object
  10. neighbor {.cursor.}: Node
  11. NodeObj = object
  12. neighbors: seq[Edge]
  13. label: string
  14. visited: bool
  15. Node = ref NodeObj
  16. Graph = object
  17. nodes: seq[Node]
  18. proc `=destroy`(x: var NodeObj) =
  19. echo x.label
  20. `=destroy`(x.neighbors)
  21. `=destroy`(x.label)
  22. proc addNode(self: var Graph; label: string): Node =
  23. self.nodes.add(Node(label: label))
  24. result = self.nodes[^1]
  25. proc addEdge(self: Graph; source, neighbor: Node) =
  26. source.neighbors.add(Edge(neighbor: neighbor))
  27. proc main =
  28. var graph: Graph
  29. let nodeA = graph.addNode("a")
  30. let nodeB = graph.addNode("b")
  31. let nodeC = graph.addNode("c")
  32. graph.addEdge(nodeA, neighbor = nodeB)
  33. graph.addEdge(nodeA, neighbor = nodeC)
  34. main()