在Julia中构建非默认构造函数 [英] Building a non-default constructor in Julia
问题描述
如何在Julia中使用少于输入值的输入构建构造函数?我有一个Int64数字数组,其中每个数字代表24个布尔值.最好的情况是,我可以发送数组并返回具有每个组件数组的复合类型.这是我尝试过的代码.
How do I build a constructor in Julia with fewer inputs than values? I have an Int64 array of numbers where each number represents 24 boolean values. The best situation would be that I could send in the array and get back a composite type with arrays of each component. Here is the code I've tried.
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值数组.
This results in a no method convert(Type{Bool},Array{Bool,1})
error. I tried to instantiate it with statuses = Status(StatusW)
where StatusW is an Int64 array of values.
有用的参考文献:类型和推荐答案
声明必须如下. 另一个引起我困惑的因素是 Another factor contributing to my confusion was that Julia Google网上论坛上的Tim Holy提供了解决方案. Tim Holy on the Julia Google Groups forum provided the solution. 完整的示例应如下所示. The full example should look like this. 这篇关于在Julia中构建非默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Valve1::Vector{Bool}
new(Valve1,...)
应该是构造函数中的最后一件事.我在new(Valve1,...)
之后添加了调试println()
行,导致该类型返回 Nothing .new(Valve1,...)
should be the last thing in the constructor. I had added debugging println()
lines after the new(Valve1,...)
causing the type to return Nothing.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