如何在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数组值。
有用的参考资料:Types和Julia documentation
将定义更改为'Valve1 :: Array {Bool,1}'会导致类似的错误。 'no method convert(Type {Array {Bool,1}},Bool)' – Jeremy