tstrict_caseobjects.nim 749 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. discard """
  2. errormsg: "field access outside of valid case branch: x.x"
  3. line: 45
  4. """
  5. {.experimental: "strictCaseObjects".}
  6. type
  7. NodeKind = enum
  8. nkParent,
  9. nkChild
  10. Node {.acyclic.} = ref object
  11. case kind: NodeKind
  12. of nkParent:
  13. children: seq[Node]
  14. of nkChild:
  15. name: string
  16. let list = @[Node(kind: nkParent, children: @[]), Node(kind: nkChild, name: "hello")]
  17. for node in list:
  18. case node.kind
  19. of nkChild:
  20. echo $node.name # here this time there is a warning
  21. else: discard
  22. type
  23. Foo = object
  24. case b: bool
  25. of false:
  26. s: string
  27. of true:
  28. x: int
  29. var x = Foo(b: true, x: 4)
  30. case x.b
  31. of true:
  32. echo x.x
  33. of false:
  34. echo "no"
  35. case x.b
  36. of false:
  37. echo x.x
  38. of true:
  39. echo "no"