Thank you, my idea wasn’t related to type-stability but to have a handle way to write multidimensional equations, like:
[V[r,p,t] = V[r,p,t-1] + Growth[r,p,t] for r in regions, p in products, t in years[2:end] ]
I can do it using normal tuples as keys, but having named tuples I thought it would have allowed me to call the variables with the dimensions in whatever order irrespectively to the original one, e.g.
[V[time=t,reg=r,prod=p] = V[r,p,t-1] + Growth[r,p,t] for r in regions, p in products, t in years[2:end] ]
This however doesn’t work as:
using NamedTuples
myTupleDict = Dict(("aa","bb") => 1, ("aa","cc") =>2)
myNamedTupleDict = Dict(@NT(one = "aa", two = "bb") =>1, @NT(one = "aa", two = "cc") => 2)
myTupleDict[("aa","bb")] # 1
myNamedTupleDict[("aa","bb")] # KeyError
myNamedTupleDict[(two="bb",one="aa")] # KeyError
I hence misinterpreted NamedTuples, as I thought that the whole point would have been to get:
@NT(one = "aa", two = "bb") == @NT(two = "bb", one = "aa") == @NT("aa", "bb") == ("aa", "bb")
My implementation of the toDict() function is as follow, but it is pretty useless given the above point:
using DataFrames, NamedTuples
df = DataFrame(
colour = ["green","blue","white","green","green"],
shape = ["circle", "triangle", "square","square","circle"],
border = ["dotted", "line", "line", "line", "dotted"],
area = [1.1, 2.3, 3.1, 4.2, 5.2]
)
function toDict(df, dimCols, valueCol)
toReturn = Dict()
equals(x, y) = Expr(:(=), x, y)
for r in eachrow(df)
keyValues = []
for d in dimCols
push!(keyValues,r[d])
end
rowKey = eval(:($NamedTuples.@NT $(equals.(dimCols, keyValues)...)))
toReturn[rowKey] = r[valueCol]
end
return toReturn
end
myDict = toDict(df,[:colour,:shape,:border],:area)
Do you think there is an easy way to obtain the desired behaviour or should I forget NamedTuples for this approach ?