# Clarification on accessing fields in a struct

**URL:** <https://discourse.julialang.org/t/clarification-on-accessing-fields-in-a-struct/60839>\
**Category:** General Usage\
**Created:** [May 9, 2021, 7:44pm UTC](https://discourse.julialang.org/t/clarification-on-accessing-fields-in-a-struct/60839 "2021-05-09T19:44:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [May 9, 2021, 7:44pm UTC](https://discourse.julialang.org/t/clarification-on-accessing-fields-in-a-struct/60839/1 "2021-05-09T19:44:22Z")

</div>

I was reading [this Stack Overflow question](https://stackoverflow.com/q/53163401), and using this example:

```julia
struct MyStruct
    a
    b
end

```

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

Two answers we given, but I want clarification on the [second](https://stackoverflow.com/a/53163471) 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()`](https://docs.julialang.org/en/v1/base/base/#Base.hasproperty) before calling the getter.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [May 9, 2021, 8:09pm UTC](https://discourse.julialang.org/t/clarification-on-accessing-fields-in-a-struct/60839/2 "2021-05-09T20:09:22Z")

</div>

> [@anon60034542](#):
>
> 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?

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.

> [@anon60034542](#):
>
> 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?

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.
