In Mathematica there is a way to create struct-like data type:
struct[myField]=value
I want to return myStruct from a function:
getStruct[input_List] := Module[{struct},
(*populate struct[key]=value from input*)
Return[struct];
];
But when I do:
st = getStruct[myInput];
DownValues[st]
it returns nothing, because it references symbol that was created inside function (like struct$1299885). I can still access this struct with st["field"], but it's not very usefully without field list.
Basically, I want to pass structs between functions, easily get field list AND easily access values inside those fields. And no, I don't want to do this with global symbols - I want to keep everything in local scope. Is it possible with Mathematica?
I tried this approach because you can easily nest structs within each other and easily access nested fields, like st["field1"]["field2"]. I can also do this with list of rules, but nested access requires a lot of code and looks ugly for something so simple.
Edit: it seems I was missing something very simple. To get list of fields I just needed to run:
DownValues[Evaluate[st]]
Thanks, Leonid.
DownValues[Evaluate[st]]
. Not sure why this was downvoted. $\endgroup$