我想知道是否有可能在一個結構內實作一個一般的參考。 代碼是:
struct Foo1
ia :: Int[/span]>
end end
結構 Foo2{UNKNOWNTYPE}。
ref :: UNKNOWNTYPE。
ib :: Int
function Foo2{UNKNOWNTYPE}(ref: :UNKNOWNTYPE,ib::Int)
o = new{UNKNOWNTYPE}(ref, ib)
回傳o
結束。
end
foo1 = Foo1()。
foo2 = Foo2{Foo1}(foo1,1)
在上面的代碼中,結構Foo2中的變數ref的型別在運行前是不確定的。上面的代碼不作業,它顯示: "LoadError("main.jl", 6, UndefVarError(:UNKNOWNTYPE))"。
uj5u.com熱心網友回復:
你基本上只是在你的建構式定義中缺少一個where UNKNOWNTYPE。我建議為Foo2使用一個外部建構式,像這樣
julia> struct Foo1
ia::Int
結束
julia> struct Foo2{T}。
ref::T
ib::Int
結束
julia> Foo2(ref::T, ib::Int) where T = Foo2{T}(ref, ib)
Foo2
julia> Foo2(Foo1(1), 1)
Foo2{Foo1}(Foo1(1), 1)
但是一個內部建構式也可以作業:
julia> struct Foo3{T}
ref::T
ib::Int
函式 Foo3(ref::T, ib::Int) where T
return new{T}(ref, ib)
結束
結束
julia> Foo3(Foo1(1), 2)
Foo3{Foo1}(Foo1(1), 2)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/316116.html
標籤:
