diff options
author | Jasper Jenkins <jasper.vs.jenkins@gmail.com> | 2019-05-26 12:22:02 -0700 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-05-26 21:22:02 +0200 |
commit | f7744260959bbdaefdc5172aaf7ff4770f8f8c03 (patch) | |
tree | 071d38af9c1e9134292a17ef197d2bd7df81b28c /doc | |
parent | 16aa10dfe101da99c402657a420dbf2785ca4a2a (diff) | |
download | Nim-f7744260959bbdaefdc5172aaf7ff4770f8f8c03.tar.gz |
Smarter variant object construction (#11273)
Diffstat (limited to 'doc')
-rw-r--r-- | doc/manual.rst | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/doc/manual.rst b/doc/manual.rst index 00635fefc..615b8e302 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -1543,6 +1543,29 @@ branch switch ``system.reset`` has to be used. Also, when the fields of a particular branch are specified during object construction, the corresponding discriminator value must be specified as a constant expression. +As a special rule, the discriminator kind can also be bounded using a ``case`` +statement. If possible values of the discriminator variable in a +``case`` statement branch are a subset of discriminator values for the selected +object branch, the initialization is considered valid. This analysis only works +for immutable discriminators of an ordinal type and disregards ``elif`` +branches. + +A small example: + +.. code-block:: nim + + let unknownKind = nkSub + + # invalid: unsafe initialization because the kind field is not statically known: + var y = Node(kind: unknownKind, strVal: "y") + + var z = Node() + case unknownKind + of nkAdd, nkSub: + # valid: possible values of this branch are a subset of nkAdd/nkSub object branch: + z = Node(kind: unknownKind, leftOp: Node(), rightOp: Node()) + else: + echo "ignoring: ", unknownKind Set type -------- |