Map and NamedTuple

I really like the fact that map keeps the keys of NamedTuple, eg

julia> map(eltype, (a = [1], b = [2.0]))
(a = Int64, b = Float64)

but I wonder it this is something that I can rely on from now on, as I could not find it in the documentation of either map or NamedTuple.

Yes, this is intended and I intend to keep it this way.

4 Likes

(Continuing this topic with a related question instead of opening a new one)

Occasionally it would be useful to have the keys available in map, in a way that meshes well with inference. I found that something like

valkeys(nt::NamedTuple{K}) where K = NamedTuple{K}(Val.(K))

works fine, eg in contexts like

map((k, aval, bval) -> dosomething, valkeys(a), a, b)

Am I missing something obvious that would achieve the same?

1 Like