diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2021-09-03 21:52:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-03 21:52:24 +0200 |
commit | cddf8ec6f684e5636a114e0e286bc6609e01f228 (patch) | |
tree | dda219b3560479c8efde6c6e90b425c1d56e51d4 /doc/manual.rst | |
parent | c2b20516d33520b1d339b447ece32ade8625fefc (diff) | |
download | Nim-cddf8ec6f684e5636a114e0e286bc6609e01f228.tar.gz |
implements https://github.com/nim-lang/RFCs/issues/407 (#18793)
Diffstat (limited to 'doc/manual.rst')
-rw-r--r-- | doc/manual.rst | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/doc/manual.rst b/doc/manual.rst index 997253f16..714f98bdc 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -1854,6 +1854,41 @@ A small example: let unknownKindBounded = range[nkAdd..nkSub](unknownKind) z = Node(kind: unknownKindBounded, leftOp: Node(), rightOp: Node()) + +cast uncheckedAssign +-------------------- + +Some restrictions for case objects can be disabled via a `{.cast(unsafeAssign).}` section: + +.. code-block:: nim + :test: "nim c $1" + + type + TokenKind* = enum + strLit, intLit + Token = object + case kind*: TokenKind + of strLit: + s*: string + of intLit: + i*: int64 + + proc passToVar(x: var TokenKind) = discard + + var t = Token(kind: strLit, s: "abc") + + {.cast(uncheckedAssign).}: + # inside the 'cast' section it is allowed to pass 't.kind' to a 'var T' parameter: + passToVar(t.kind) + + # inside the 'cast' section it is allowed to set field 's' even though the + # constructed 'kind' field has an unknown value: + t = Token(kind: t.kind, s: "abc") + + # inside the 'cast' section it is allowed to assign to the 't.kind' field directly: + t.kind = intLit + + Set type -------- |