summary refs log tree commit diff stats
path: root/doc/manual.rst
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2021-09-03 21:52:24 +0200
committerGitHub <noreply@github.com>2021-09-03 21:52:24 +0200
commitcddf8ec6f684e5636a114e0e286bc6609e01f228 (patch)
treedda219b3560479c8efde6c6e90b425c1d56e51d4 /doc/manual.rst
parentc2b20516d33520b1d339b447ece32ade8625fefc (diff)
downloadNim-cddf8ec6f684e5636a114e0e286bc6609e01f228.tar.gz
implements https://github.com/nim-lang/RFCs/issues/407 (#18793)
Diffstat (limited to 'doc/manual.rst')
-rw-r--r--doc/manual.rst35
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
 --------