Clarification on accessing fields in a struct

I was reading this Stack Overflow question, and using this example:

struct MyStruct

it asks how you can access a struct field using a String.

Two answers we given, but I want clarification on the second answer.

  1. As stated in the answer, it’s type unstable. It’s because a and b could be different types, and we don’t know exactly what type doSomething() will return, correct?
  2. Two answers were provided and while they work, I thought maybe the more simple and obvious solution was to write a getter i.e getA(). Would that work? The user of the struct obviously knows the field they’re after, and you use hasproperty() before calling the getter.

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.