Skip to content

Const variant object fields are incorrectly set to zero #15218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
exelotl opened this issue Aug 23, 2020 · 2 comments
Closed

Const variant object fields are incorrectly set to zero #15218

exelotl opened this issue Aug 23, 2020 · 2 comments

Comments

@exelotl
Copy link
Contributor

exelotl commented Aug 23, 2020

When a variant object is bound to a const, the latter fields are initalised to 0, which overwrites the former fields.

Example

type
  Kind = enum
    A, B, C
  Rocket = object
    case kind: Kind
    of A: a: int
    of B: b: int
    of C: c: int

const a = Rocket(kind: A, a: 20)
const b = Rocket(kind: B, b: 30)
const c = Rocket(kind: C, c: 40)

echo a
echo b
echo c

Current Output

(kind: A, a: 0)
(kind: B, b: 0)
(kind: C, c: 40)

Expected Output

(kind: A, a: 20)
(kind: B, b: 30)
(kind: C, c: 40)

Additional Information

This only seems to happen when using const
i.e. the following are fine:

let a = Rocket(kind: A, a: 20)
echo a                        # (kind: A, a: 20)
echo Rocket(kind: B, b: 30)   # (kind: B, b: 30)

$ nim -v
Nim Compiler Version 1.2.6 [Linux: amd64]
@shirleyquirk
Copy link
Contributor

Duplicate of #13081, link #8007 #8015
See https://forum.nim-lang.org/t/6530 for workarounds

@ghost
Copy link

ghost commented Aug 23, 2020

Closing as a duplicate of #13081 :)

@ghost ghost closed this as completed Aug 23, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants