I’m trying to do something about physics, and I’ve written a struct like
struct System
charge_density
end
now there is something called “spin” that may, but not must be considered in physics, and the data structure will be different. It looks something like
struct SystemNoSpin
charge_density::Array{Float64, 3}
end
struct SystemWithSpin
charge_density::Array{Float64, 4}
end
conventionally, we will set a variable called “SPIN” = 1 when not considering it, and 2 when considering. So I try to use parametric types. I found this is a practical way:
struct System{SPIN}
charge_density::Array{Float64, SPIN+2}
end
but this is not:
charge_type = Dict{Int, Type}([(1, Array{Float64, 3}), (2, Array{Float64, 4})])
struct System{SPIN}
charge_density::charge_type[SPIN]
end
it will raise “KeyError:: key SPIN not found”
However, I feel that the second way, if available, seems to be more readable. Any way to do it?
Maybe I should make it more clear what I want. In physics, there are many properties, and they may be categorized into many kinds. For example, all space-related properties are Array{T, 3} without spin and Array{T, 4} with spin, while all band-related properties are Array{T, 2} without spin and Array{T, 3} with spin. T maybe either Float64 or ComplexF64. That’s why I think the second way mentioned above is more readable. In that way I can define some type alias, some dictionaries, then write
struct System{SPIN}
property1::space_related[SPIN]{Float64}
property2::space_related[SPIN]{ComplexF64}
property3::band_related[SPIN]{Float64}
end
On the contrary, in the first way I mentioned above, it can only be written as
struct System{SPIN}
property1::Array{Float64, 2 + SPIN}
property2::Array{ComplexF64, 2 + SPIN}
property3::Array{Float64, 1 + SPIN}
end
And now I’ve turned to a third solution:
struct SpaceRelatedProperties{SPIN}
property1::Array{Float64, 2 + SPIN}
property2::Array{ComplexF64, 2 + SPIN}
end
struct BandRelatedProperties{SPIN}
property3::Array{Float64, 1 + SPIN}
end
struct System{SPIN}
s_p::SpaceRelatedProperties{SPIN}
b_p::BandRelatedProperties{SPIN}
end
Any better ways?