tfuncs_cannot_mutate3.nim 645 B

123456789101112131415161718192021222324252627282930313233343536
  1. discard """
  2. errormsg: "cannot mutate location kid.parent within a strict func"
  3. line: 16
  4. """
  5. {.experimental: "strictFuncs".}
  6. type
  7. Node = ref object
  8. name: string
  9. kids: seq[Node]
  10. parent: Node
  11. func initParents(tree: Node) =
  12. for kid in tree.kids:
  13. kid.parent = tree
  14. initParents(kid)
  15. proc process(intro: Node): Node =
  16. var tree = Node(name: "root", kids: @[
  17. intro,
  18. Node(name: "one", kids: @[
  19. Node(name: "two"),
  20. Node(name: "three"),
  21. ]),
  22. Node(name: "four"),
  23. ])
  24. initParents(tree)
  25. proc main() =
  26. var intro = Node(name: "intro")
  27. var tree = process(intro)
  28. echo intro.parent.name
  29. main()