How do I call a function in a lookup table while ensure type stability?

Some options:

  1. Instead of storing var::Symbol in your DerivedArray, store the actual function that you will use to compute the value. Better yet, use: GitHub - JuliaArrays/MappedArrays.jl: Lazy in-place transformations of arrays

  2. Make var a type parameter V rather than a field, and dispatch on it. e.g. struct DerivedArray{V, T, N} <: AbstractArray{T, N}, and then dispatch on it. e.g. for each operation foo, define Base.getindex(x::DerivedArray{:Foo}, I...) = get_foo(x.base, I...)

  3. Same as (2), but instead of making V a Symbol like :Foo, make it a type. This gives you more flexibility because you can add parameters to the type, do other things besides call it, and so forth.

But in all three cases the key is to define the desired operation as a part of the type of your array, and then dispatch on it. This allows the compiler to specialize, infer the type of the result, and so forth.

4 Likes