Writing a static value-keyed container with dynamic lookup when inlining isn't possible

Hi all,

I’m experimenting with a container that is conceptually similar to a NamedTuple, except that the “keys” are arbitrary isbits values, not Symbols.

struct ValueNamedTuple{Vals,K}
    vals::Vals                          # e.g. tuple of (Val(key) => value) pairs
    dynamic_lookup::Dict{K,Int}         # key -> index into vals
end

What I would like:

  • If the key is known at compile time → use a generated/static lookup (generated linear search and inline the index).
  • If the key is only known at runtime → use a single dynamic lookup path (e.g. via the Dict) so that we don’t have to rely on linear search.

e.g.

@generated function getproperty(vnt::ValueNamedTuple, v::Val{value}) where value
      idx = findfirs(x -> value == x, get_types_of_vals_from_vnt_type(vnt))
      return :(last(getindex(vnt.vals, $idx)))
end

but then if the value is not a compile-time constant, just do

getproperty(vnt::ValueNamedTuple, value) = last(getindex(vnt.values, vnt.dynamic_lookup[value]))

where the Dict is filled during construction.

Is this in any way possible?