Reputation: 631
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
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.
Useful references: Types and Constructors section of the Julia documentation
Upvotes: 3
Views: 867
Reputation: 631
The declarations needs to be as follows.
Valve1::Vector{Bool}
Another factor contributing to my confusion was that 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.
Tim Holy on the Julia Google Groups forum provided the solution.
The full example should look like this.
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
Upvotes: 2
Reputation: 3783
The error message is correct, but unfortunately it is quite hard to understand the generic error messages in Julia.
The problem is that you declare your fields as the partially initialized Array{Bool, N}
, and that does not seem to work when you try to call the constructor with Array{Bool, 1}
.
The right solution is to declare the type to contain a fully initialized type Array{Bool,1}
or use the alias Vector{Bool}
.
What version of Julia are you using? The code you posted works for me on the latest Julia master, and I think this might have been fixed when https://github.com/JuliaLang/julia/issues/4026 solved.
Upvotes: 0