例如,我希望有如下的类型A与B:
mutable struct A
a::Int
b::Ref{B}
end
mutable struct B
b::Int
a::Ref{A}
end
当然,这是错误的定义,因为A用到了未定义的B!!!
解决这类问题的办法就是不完整类型初始化!(见Julia手册Incomplete Initialization
)
方法如下:
mutable struct A{T}
a::Int
b::Ref{T}
A{T}(a::Int) where T = new{T}(a)
end
mutable struct B
b::Int
a::Ref{A{T}} where T
B(b::Int)=new(b)
end
a =A{B}(1)
b = B(2)
a.b=Ref{B}()
a.b[]=b
a.b[].b == 2
b.a=Ref{A{B}}()
b.a[] = a
b.a[].a == 1