Reputation: 319
I have a derived type represented by:
struct BoundaryCondition
domain::MeshStructure
left::BorderValue
right::BorderValue
bottom::BorderValue
top::BorderValue
back::BorderValue
front::BorderValue
end
I want to create an array of this derived type, of size n_comp, which I have in my code as:
bc = Array{BoundaryCondition}(n_comp)
for i in 1:n_comp
bc[i] = createBC(m)
bc[i].left.a[:] = 0.0
bc[i].left.b[:] = 1.0
bc[i].left.c[:] = c_left[i]
end
But I receive an error:
ERROR: LoadError: MethodError: no method matching (Array{BoundaryCondition, N} where N)(::Int64)
What is the correct way to declare my array of type BoundaryCondition?
Upvotes: 3
Views: 83
Reputation: 3015
If you want to preallocate it, you need to fill your array with something. For example, you can use undef
(replace the Bool
type and size with those of your problem):
julia> Vector{Bool}(undef, 3)
3-element Vector{Bool}:
1
0
0
You can also use fill
(same, replace false
with your problem's default "fill" value):
julia> fill(false, 3)
3-element Vector{Bool}:
0
0
0
If you don't need to preallocate, a good solution is comprehensions. Make a function of the index for creating your boundary condition, e.g.:
function my_BC(i)
out = createBC(m)
out.left.a[:] = 0.0
out.left.b[:] = 1.0
out.left.c[:] = c_left[i]
return out
end
and then simply do
[my_BC(i) for i in 1:n_comp]
Upvotes: 2