This commit is contained in:
Andreas Rumpf 2021-09-03 21:52:24 +02:00 • committed by GitHub
commit cddf8ec6f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 103 additions and 20 deletions

View file

@ -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
--------