在Julia中构建非默认构造函数

在Julia中构建非默认构造函数,julia,Julia,如何在Julia中构建输入少于值的构造函数?我有一个Int64数字数组,其中每个数字代表24个布尔值。最好的情况是,我可以发送数组并返回一个包含每个组件数组的复合类型。这是我试过的代码 type Status Valve1::Array{Bool} Valve2::Array{Bool} Valve3::Array{Bool} Valve4::Array{Bool} Valve5::Array{Bool} Valve6::Array{Bool} Valv

如何在Julia中构建输入少于值的构造函数?我有一个Int64数字数组,其中每个数字代表24个布尔值。最好的情况是,我可以发送数组并返回一个包含每个组件数组的复合类型。这是我试过的代码

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
这将导致
无方法转换(类型{Bool},数组{Bool,1})
错误。我尝试用
statuses=Status(StatusW)
实例化它,其中StatusW是一个Int64值数组


有用的参考资料:

部分的错误消息是正确的,但不幸的是,很难理解Julia中的一般错误消息

问题是您将字段声明为部分初始化的
数组{Bool,N}
,当您尝试使用
数组{Bool,1}
调用构造函数时,这似乎不起作用

正确的解决方案是将类型声明为包含完全初始化的类型
数组{Bool,1}
,或者使用别名
向量{Bool}


你用的是什么版本的朱莉娅?您发布的代码在最新的Julia master上对我有效,我想这可能在解决后得到修复

声明需要如下所示

Valve1::Vector{Bool}
另一个导致我困惑的因素是
new(Valve1,…)
应该是构造函数中的最后一项。我在
new(Valve1,…)
之后添加了调试
println()
行,导致类型不返回任何内容

Tim Holy在Julia Google Group论坛上提供了

完整的示例应该如下所示

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.2.0(2013-11-16 23:44 UTC)将定义更改为
Valve1::Array{Bool,1}
会导致类似错误<代码>无方法转换(类型{Array{Bool,1}},Bool)