Help to remove type instability when using getproperty

I found a way!

mutable struct Workspace{T}
    x::Vector{T}
    eclipse::Bool
    s_i::Vector{T}
    s_b::Vector{T}
end

# Functions to translate the state-vector into variables.
@inline Base.getproperty(a::Workspace, f::Symbol) = getindex(a, Val(f))
@inline Base.getindex(a::Workspace, s::Val) = __getindex(a,s)
@inline __getindex(a::Workspace, ::Val{:JD}) = getfield(a,:x)[1]
@inline __getindex(a::Workspace, ::Val{:w}) = view(getfield(a,:x), 2:4)
@inline __getindex(a::Workspace, ::Val{F}) where F = getfield(a,F)

No type instabilities :slight_smile: Thanks @ChrisRackauckas

2 Likes