implements https://github.com/nim-lang/RFCs/issues/407 (#18793)
This commit is contained in:
parent
c2b20516d3
commit
cddf8ec6f6
9 changed files with 103 additions and 20 deletions
|
|
@ -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
|
||||
--------
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue