Clarification on accessing fields in a struct

In general, getproperty(x, p) or getfield(x, p) are type unstable, unless all fields of x have the same concrete type. In practice, however, the property name is often a compile-time constant (e.g. when x.a syntax is used), so the compiler takes advantage of that to produce type stable code.

Yes, and it should be the preferred way most of the time, but it seems to me that if one needs to access a field using a string, that is not a useful solution.

My use cases for such an operation involved constructing a named tuple with names read from a file, and then accessing the fields based on a file input as well. Obviously, you can’t write an accessor in advance in such case.

2 Likes