Hi, here is what I am trying to do
abstract type AbstractParticleProperties end
mutable struct Particle{N, S, T}
properties::ParticleProperties{N, T}
states::ParticleStates{S, T}
end
mutable struct ParticleProperties{N <:Int, T <:Real} <:AbstractParticleProperties
id::N
mass::T
#More fields ...
end
mutable struct ParticleStates{S <:Int, T <:Real}
x::SVector{S, T}
f::SVector{S, T}
#More fields...
end
In my code I typically need to access kind or id of a particle (an instance of Particle). Originally, my desire was that I don’t want to always write particle.properties.(some field in particle properties). So I implement sth like
function get_properties_field(particle::Particle{N, S, T}, s::Symbol) where {N, S, T}
getfield(getfield(particle, properties), s)
end
which I discovered that it is not typed stable. After reading what you have described, I implements sth like this instead
function id(particle::Particle{N, S, T}) where {N, S, T}
particle.properties.id
end
When I measure the performance, it turns out that the second version performance a lot better . However, this means that I have to write sth like this for every fields in the ParticleProperties. Moreover, every time that I want
create a subtype of AbstractParticleProperties with a different field, says electric charge. I have to implement sth like above again. Is there a nice way work around this? Alternatively, I don’t have to think about this problem just use p.properties.(whatever). Hopefully, this clarify up my question above.