# Is this generated function for struct2tuple safe to use?

**URL:** https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086
**Category:** General Usage
**Tags:** runtimegeneratedfunc, generated
**Created:** [September 4, 2025, 1:42am UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086 "2025-09-04T01:42:36Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)
#### Post date: [September 4, 2025, 1:42am UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/1 "2025-09-04T01:42:36Z")

</div>

I’m not really sure if this function could be used safely when I have a struct with mutable fields in it:

```julia-auto
@generated function struct2tuple(x::T) where T
    n = fieldcount(T)
    exprs = [:(getfield(x, $(i))) for i in 1:n]
    return Expr(:tuple, exprs...)
end

```

is it forbidden? It seems that the manual says something against this when talking about observing mutable states, but it seems to work fine e.g.

```julia-auto
julia> struct A
           x::Vector{Int}
           y::Vector{Float64}
       end

julia> struct2tuple(A([1], [1.]))
([1], [1.0])

```

But I’m a total beginner in these kind of things so I’m unsure.

---

<div class="post-metadata">

### Author: ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)
#### Post date: [September 4, 2025, 2:11am UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/2 "2025-09-04T02:11:07Z")

</div>

Maybe I’m wrong, but if “observing mutable states” means that I base any code generation decision on mutable values, then this should be safe.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 4, 2025, 2:34am UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/3 "2025-09-04T02:34:45Z")

</div>

> Generated functions must not _mutate_ or _observe_ any non-constant global state (including, for example, IO, locks, non-local dictionaries, or using [`hasmethod`](https://docs.julialang.org/en/v1/base/base/#Base.hasmethod)).

you’re fine I think, you’re not mutating any non-constant global state, and I don’t think `fieldcount(T)` is a mutable global state.

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [September 4, 2025, 4:08am UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/4 "2025-09-04T04:08:43Z")

</div>

technically `fieldcount` is a generic function which could be overloaded for a specific type.  
But if that happens a lot of other things will probably break,  
so you are already in hell.  
This code is fine basically.

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [September 4, 2025, 5:42am UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/5 "2025-09-04T05:42:04Z")

</div>

This doesn’t answer your question, but in your specific case you can replace the generated function by

```julia-auto
struct2tuple(x::T) where T = ntuple(i -> getfield(x, i), Val(fieldcount(T)))

```

(I didn’t check that this remains efficient for structs with very many fields.)

---

<div class="post-metadata">

### Author: ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)
#### Post date: [September 4, 2025, 1:22pm UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/6 "2025-09-04T13:22:53Z")

</div>

Nice, yes, it seems to work fine in my use case. I had the same function without the `Val` and it wasn’t working efficiently, but with that it does.

Thanks a lot to all of you!

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [September 4, 2025, 10:42pm UTC](https://discourse.julialang.org/t/is-this-generated-function-for-struct2tuple-safe-to-use/132086/7 "2025-09-04T22:42:56Z")

</div>

Also check ConstructionBase.jl: it has `getfields(your_obj)` and `getproperties(your_obj)`.  
It’s a very lightweight package, a large fraction of Julia ecosystem depends on it.
