Hello, here is a MWE:
mutable struct Bar{T}
y::T
foo
end
Bar(y) = Bar(y, nothing)
mutable struct Foo{T, B <: Bar}
x::T
bar::B
end
function Foo(x, bar)
foo = Foo(x, bar)
bar.foo = foo
return foo
end
bar = Bar(2.)
foo = Foo(10,bar)
bar.foo.x
Basically I would like to figure out a way to have two instances, one of each struct, to refer to one another. So, despite the type instability that might ensue, I made Bar with an free typed foo field. When I itinitialize bar, I initialize as nothing and when I instantiate a Foo, it changes the reference of bar.foo from nothing to foo. There’s no error during construction, but the last line returns
ERROR: type Nothing has no field x
Stacktrace:
[1] getproperty(::Nothing, ::Symbol) at .\Base.jl:33
[2] top-level scope at REPL[2]:1
So I tried instead to use incomplete initialization
mutable struct Bar{T}
y::T
foo
Bar(y) = new(y)
end
But then I get the error
ERROR: syntax: too few type parameters specified in "new{...}" around REPL[3]:1
Stacktrace:
[1] top-level scope at REPL[3]:1
I don’t understand, that’s the point of incomplete initialization to have fewer fields…
Note that I can’t know in advance which type parameters bar.foo will have. In fact, I need bar.foo to be able to accept other structs than Foo.
EDIT: There was a y instead of x but that was not the problem.