2014-02-24 55 views
1

如何在Julia中使用少於輸入值的輸入構建構造函數?我有一個Int64數組數組,其中每個數字代表24個布爾值。最好的情況是我可以發送數組並獲取每個組件的數組的複合類型。這是我試過的代碼。在Julia中構建非默認構造函數

type Status 
    Valve1::Array{Bool} 
    Valve2::Array{Bool} 
    Valve3::Array{Bool} 
    Valve4::Array{Bool} 
    Valve5::Array{Bool} 
    Valve6::Array{Bool} 
    Valve7::Array{Bool} 
    Valve8::Array{Bool} 

    # Constructor for Status type 
    function Status(vals::Array{Int64}) 
    l = int64(length(vals)) 

    Valve1 = Array(Bool,l) 
    Valve2 = Array(Bool,l) 
    Valve3 = Array(Bool,l) 
    Valve4 = Array(Bool,l) 
    Valve5 = Array(Bool,l) 
    Valve6 = Array(Bool,l) 
    Valve7 = Array(Bool,l) 
    Valve8 = Array(Bool,l) 

    # Parse Inputs 
    for i=1:l 
     # Byte 1 
     Valve1[i] = vals[i] & 2^(1-1) > 0 
     Valve2[i] = vals[i] & 2^(2-1) > 0 
     Valve3[i] = vals[i] & 2^(3-1) > 0 
     Valve4[i] = vals[i] & 2^(4-1) > 0 
     Valve5[i] = vals[i] & 2^(5-1) > 0 
     Valve6[i] = vals[i] & 2^(6-1) > 0 
     Valve7[i] = vals[i] & 2^(7-1) > 0 
     Valve8[i] = vals[i] & 2^(8-1) > 0 
    end # End of conversion 

    new(Valve1,Valve2,Valve3,Valve4,Valve5,Valve6,Valve7,Valve8) 

    end # End of constructor 
end # End of type 

這會導致no method convert(Type{Bool},Array{Bool,1})錯誤。我試圖用statuses = Status(StatusW)實例化它,其中StatusW是一個Int64數組值。

有用的參考資料:TypesJulia documentation

+0

將定義更改爲'Valve1 :: Array {Bool,1}'會導致類似的錯誤。 'no method convert(Type {Array {Bool,1}},Bool)' – Jeremy

回答

1

聲明需要如下。

Valve1::Vector{Bool} 

有助於我的困惑的另一個因素是new(Valve1,...)應該是在構造函數中的最後一件事。我在new(Valve1,...)之後添加了調試println()行,導致類型返回Nothing

Tim Holy在Julia Google Groups論壇提供了solution

完整的例子應該是這樣的。

type Status 
    Valve1::VectorBool} 
    Valve2::Vector{Bool} 
    Valve3::Vector{Bool} 
    Valve4::Vector{Bool} 
    Valve5::Vector{Bool} 
    Valve6::Vector{Bool} 
    Valve7::Vector{Bool} 
    Valve8::Vector{Bool} 

    # Constructor for Status type 
    function Status(vals::Array{Int64}) 
     l = int64(length(vals)) 

     Valve1 = Array(Bool,l) 
     Valve2 = Array(Bool,l) 
     Valve3 = Array(Bool,l) 
     Valve4 = Array(Bool,l) 
     Valve5 = Array(Bool,l) 
     Valve6 = Array(Bool,l) 
     Valve7 = Array(Bool,l) 
     Valve8 = Array(Bool,l) 

     # Parse Inputs 
     for i=1:l 
      # Byte 1 
      Valve1[i] = vals[i] & 2^(1-1) > 0 
      Valve2[i] = vals[i] & 2^(2-1) > 0 
      Valve3[i] = vals[i] & 2^(3-1) > 0 
      Valve4[i] = vals[i] & 2^(4-1) > 0 
      Valve5[i] = vals[i] & 2^(5-1) > 0 
      Valve6[i] = vals[i] & 2^(6-1) > 0 
      Valve7[i] = vals[i] & 2^(7-1) > 0 
      Valve8[i] = vals[i] & 2^(8-1) > 0 
     end # End of conversion 

     new(Valve1,Valve2,Valve3,Valve4,Valve5,Valve6,Valve7,Valve8) 

    end # End of constructor 
end # End of type 
0

錯誤消息的Constructors部分是正確的,但不幸的是挺難理解朱莉婭的一般錯誤消息。

問題是,您聲明您的字段爲部分初始化的Array{Bool, N},並且當您嘗試使用Array{Bool, 1}調用構造函數時似乎不起作用。

正確的解決方案是聲明類型包含完全初始化類型Array{Bool,1}或使用別名Vector{Bool}

你使用的是哪個版本的Julia?您發佈的代碼適用於最新的朱莉婭大師,我認爲這可能在https://github.com/JuliaLang/julia/issues/4026解決了。

+0

Version 0.2.0(2013-11-16 23:44 UTC) – Jeremy