I am trying to write a non-mutating setindex for named tuple, where given a value type for the name var, it creates a new named tuple where x.var = y. What I have right now is:
function setindex(nt::T,x,v::Val{var}) where {T,var}
if var ∉ fieldnames(T)
return x
else
typeof(x)(s == var ? y : w for (s,w) in nt)
end
end
but my main issue is that I'm not sure of a good way to iterate the named tuple to get the (name,value) pairs.