Skip to content

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

@sandwoodK

Description

@sandwoodK

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions