tfieldchecks.nim 678 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. discard """
  2. output: '''
  3. foo
  4. C
  5. 3.14
  6. foo
  7. 3.14
  8. 3.14
  9. '''
  10. """
  11. type
  12. V = enum
  13. A, B, C
  14. X = object
  15. f0: string
  16. case f1: V
  17. of A: f2: string
  18. of B: discard
  19. of C: f3: float
  20. var obj = X(f0: "foo", f1: C, f3: 3.14)
  21. block:
  22. echo obj.f0
  23. echo obj.f1
  24. doAssertRaises(FieldDefect): echo obj.f2
  25. echo obj.f3
  26. block:
  27. let a0 = addr(obj.f0)
  28. echo a0[]
  29. # let a1 = addr(obj.f1)
  30. # echo a1[]
  31. doAssertRaises(FieldDefect):
  32. let a2 = addr(obj.f2)
  33. echo a2[]
  34. let a3 = addr(obj.f3)
  35. echo a3[]
  36. # Prevent double evaluation of LHS
  37. block:
  38. var flag = false
  39. proc wrap(x: X): X =
  40. doAssert flag == false
  41. flag = true
  42. result = x
  43. echo wrap(obj).f3