cast uncheckedAssign

Some restrictions for case objects can be disabled via a {.cast(uncheckedAssign).} section:

  1. type
  2. TokenKind* = enum
  3. strLit, intLit
  4. Token = object
  5. case kind*: TokenKind
  6. of strLit:
  7. s*: string
  8. of intLit:
  9. i*: int64
  10. proc passToVar(x: var TokenKind) = discard
  11. var t = Token(kind: strLit, s: "abc")
  12. {.cast(uncheckedAssign).}:
  13. # inside the 'cast' section it is allowed to pass 't.kind' to a 'var T' parameter:
  14. passToVar(t.kind)
  15. # inside the 'cast' section it is allowed to set field 's' even though the
  16. # constructed 'kind' field has an unknown value:
  17. t = Token(kind: t.kind, s: "abc")
  18. # inside the 'cast' section it is allowed to assign to the 't.kind' field directly:
  19. t.kind = intLit