Why isn't getproperty defined for Dictionaries?

Ehh, programming styles that only work for a special cases (or require some “non-standard tricks” like @aplavin showed) are generally something I’d avoid. There’s other issues here too though. x.foo has literal information, and so if you know all of the fields already and have that information at compile-time, why not use a NamedTuple? It would be safer, because x.foo2 = y would create a new field in the Dictionary while it would error with the NamedTuple, so if you know all of the fields at compile-time well enough to write x.foo then you probably want error checking. If you don’t know the field at compile-time, then you’d do getproperty(x,:foo), in which case you might as well do x[:foo].

So I just don’t see where the utility is: either it’s dynamic so use a Dictionary and because it’s dynamic names you need to x[sym], or it’s static so you can x.foo in which case you can construct it like (;foo = y).

7 Likes