Skip to content

Object field initialized with an object constructor that is declared later fail to compile #25784

@demotomohiro

Description

@demotomohiro

Nim Version

Nim Compiler Version 2.3.1 [Linux: arm64]
Compiled at 2026-04-30
Copyright (c) 2006-2026 by Andreas Rumpf

git hash: 92d0c09
active boot switches: -d:release

Description

Compiling following code generates compile error: object constructor needs an object type [forward declared in testobjcon3.nim(5, 3)]

type
  Foo = object
    x = Bar()

  Bar = object
    x: int

var f = Foo()
echo f.x

Current Output

testobjcon3.nim(3, 12) Error: object constructor needs an object type [forward declared in testobjcon3.nim(5, 3)]

Expected Output

Compiles without error

Known Workarounds

Declaring Bar before Foo compiles without error:

type
  Bar = object
    x: int

  Foo = object
    x = Bar()

var f = Foo()
echo f.x

Additional Information

No response

Metadata

Metadata

Assignees

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