Skip to content
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

Why object field initialization does not work with proc or func result ? #24771

Open
sandwoodK opened this issue Mar 11, 2025 · 1 comment
Open

Comments

@sandwoodK
Copy link

Nim Version

2.2

Description

Certainly not a bug but coming from other programming languages it feels buggy that this snippet

type Foo = object
  port* : Natural = 6789

let f = Foo()
echo "f.port:", f.port

proc someStuff() : Foo =
  echo "result.port:" , result.port
  result = Foo()
  echo "result after build.port:" , result.port

let sf = someStuff()
echo "sf.port:", sf.port

outputs
f.port:6789
result.port:0
result after build.port:6789
sf.port:6789

IMO it is both error proning and not consistent...
what is the rationale, is there any magic compilation flag to have field initialization done 'ever ever' ?

Thanks.

Current Output


Expected Output


Known Workarounds

No response

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Mar 12, 2025

--experimental:strictDefs enforces explicit initialization

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